再次发现父类构造函数只会执行一次
父类 B
package gavin;
class B{
public B(){
System.out.println("B Constructor B()");
}
public B(int i){
this();
System.out.println("B Constructor B(int i)");
}
}
子类C:
package gavin;
class C extends B{
int i = v();
int j;
{
j = 30;
System.out.println("初始块中的代码");
}
public C(){
this(10);
System.out.println("this is C Constructor C()");
}
public C(int i){
System.out.println("this is C Constructor C(int i)");
}
int v(){
System.out.println("使用初始化程序和初始块初始化成员");
return 10;
}
}
执行结果:
B Constructor B()
使用初始化程序和初始块初始化成员
初始块中的代码
this is C Constructor C(int i)
this is C Constructor C()
执行结果与递归形式结果类似。