Chapter 2. Object-Oriented Programming
Set(Mutator Methods)方法改变对象的状态,Get(accessor methods)方法则不;
Java中变量不持有对象,他们引用对象;
变量的实例和方法的实现在类的声明部分声明;
对象调用自身实例化的方法,靠this引用
5. 构造方法与类同名,一个类可以拥有多个构造方法;
6. 静态变量不属于任何对象,静态方法不通过对象调用
7. 类之间通过包来组织,使用import声明使得你不需要在程序里写完整的包名;
8. 类可以嵌套;
9. 内部类是非静态的嵌套类,其实例拥有构造过的嵌入类的引用;
10. Java doc工具处理源文件产生HTML格式的代码注释;
基本概念:
encapsulation 封装
inheritance 继承
polymorphism 多态
mutator => setter
accessor => getter
Java8 多了一种参数方式,receiver parameter
我们可以使用this作为参数,但是要保证JDK版本,否则
jls有详细说明:
http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.1
Here are some examples of receiver parameters in instance methods and inner classes' constructors:
class Test {
Test(/* ?? ?? */) {}
// No receiver parameter is permitted in the constructor of
// a top level class, as there is no conceivable type or name.
void m(Test this) {}
// OK: receiver parameter in an instance method
static void n(Test this) {}
// Illegal: receiver parameter in a static method
class A {
A(Test Test.this) {}
// OK: the receiver parameter represents the instance
// of Test which immediately encloses the instance
// of A being constructed.
void m(A this) {}
// OK: the receiver parameter represents the instance
// of A for which A.m() is invoked.
class B {
B(Test.A A.this) {}
// OK: the receiver parameter represents the instance
// of A which immediately encloses the instance of B
// being constructed.
void m(Test.A.B this) {}
// OK: the receiver parameter represents the instance
// of B for which B.m() is invoked.
}
}
}
B's constructor and instance method show that the type of the receiver parameter may be denoted with a qualified TypeName like any other type; but that the name of the receiver parameter in an inner class's constructor must use the simple name of the enclosing class.
内部类不可以拥有静态变量和静态方法,但是可以拥有常量。
public class Test {
class TestClass {
private final static int num = 0;// right
private static int id = 0;//Error:(17, 28) java: Illegal static declaration in inner class com.scb.smarttrans.utils.generator.Test.TestClass
modifier 'static' is only allowed in constant variable declarations
public static int addOne() {
return ++id;
} //Error:(19, 27) java: Illegal static declaration in inner class com.scb.smarttrans.utils.generator.Test.TestClass
modifier 'static' is only allowed in constant variable declarations
}
}
作者趁机小黑了一把Java Designer
Inner classes cannot declare static members other than compile-time constants. There would be an ambiguity about the meaning of “static.” Does it mean there is only one instance in the virtual machine? Or only one instance per outer object? The language designers decided not to tackle this issue.
文档注释有些不常见的用法:
/**
* An <code>Invoice</code> object represents an invoice with
* line items for each part of the order.
*
* @param byPercent the percentage by which to raise the salary (e.g., 10 means 10%)
* @author Fred Flintstone
* @author Barney Rubble
* @version 1.1
* <p>
* Raises the salary of an employee.
* @return the amount of the raise
* @see com.horstmann.corejava.Employee#raiseSalary(double)
* @see <a href="http://en.wikipedia.org/wiki/Leap_year">Leap years</a>.
* @see "Core Java for the Impatient"
* {@link package.class#feature label}
* <img src="doc-files/uml.png" alt="UML diagram"/>.
* @since version 1.7.1
* @deprecated Use <code>setVisible(true)</code> instead
*/