class demo extends B{//6:对本类初始化
private A m = new A();//7:初始化A对象对应的类
private B b = new B();//9:初始化B对象对应的类
private C c = new C();//14:初始化C对象对应的类
public test2() {
System.out.println("new demo");//16:执行test2构造函数,打印Demo
}
public static void main(String[] args) {
new test2();//1:读main中的这句,初始化test2对象对应的类
}
}
class A{//3:再上层父类初始化. 11:父类初始化
public A() {//4:执行再上层父类构造函数,打印一个A 8:调用A构造函数,打印A 12:执行A构造函数,打印A
System.out.println("new A");
}
}
class B extends A{//2:父类初始化 10:初始化B类
public B() {//5:执行父类构造函数,打印一个B 13:执行B构造函数,打印B
System.out.println("new B");
}
}
class C{
public C() {//15:执行C构造函数,打印C
System.out.println("new C");
}
} |