除了良好的面向对象设计之外,这个星座不违反任何其他规则。Java 已为这种情况做好了准备,因为 B 的所有字段甚至在调用任何构造函数之前都已初始化,如以下示例所示:
public class A {
int aMember;
public static final void main(String[] args) {
new B();
}
A() {
B b = (B) this;
System.out.println("in A(), before b.bMember has been set here, b.bMember = " + b.bMember);
System.out.println("in A(), before this.aMember has been set here, this.aMember = " + this.aMember);
this.aMember = 5;
b.bMember = 1; // will be overwritten in B()
System.out.println("in A(), after b.bMember has been set here, b.bMember = " + b.bMember);
System.out.println("in A(), after this.aMember has been set here, this.aMember = " + this.aMember);
}
}
--
public class B extends A {
int bMember;
B() {
super(); // invokes A()
System.out.println("in B(), before this.bMember has been set here, this.bMember = " + this.bMember);
this.bMember = 6;
System.out.println("in B(), after this.bMember has been set here, this.bMember = " + this.bMember);
}
}
这输出:
in A(), before b.bMember has been set here, b.bMember = 0
in A(), before this.aMember has been set here, this.aMember = 0
in A(), after b.bMember has been set here, b.bMember = 1
in A(), after this.aMember has been set here, this.aMember = 5
in B(), before this.bMember has been set here, this.bMember = 1
in B(), after this.bMember has been set here, this.bMember = 6
这意味着在 A() 中,B 的成员变量在设置之前与 A 的成员变量处于相同的状态。两者都已初始化为其默认值(0 表示 int,false 表示布尔值,null 表示对象,...)并且可以使用。一个很大的坑是B的成员变量可以在A()中设置,但是可以被B自己的构造函数覆盖,这很违反直觉。
我喜欢这个问题,因为它表达得简单明了,但它提出了更复杂的问题,这些问题涉及到其他主题,如编译器内部和对象创建的细节。