public class Answer {
public static void main(String []args) {
Before x = new After();
System.out.println("--------");
//Before y = new After();
}
}
class Before {
Before() {
print();
System.out.println("-----------");
pub = 5;
}
void print() {System.out.println("("+ pub + ")"); }
class Printf {Printf(){System.out.println("print!!!!!");}}
protected int pub = -1;
protected Printf prin = new Printf();
protected String str = new String("before");
}
class After extends Before {
After() {
super();
print();
}
void print() {System.out.print(pri);System.out.println("("+ pub + ")"); System.out.println(sta);System.out.println(str);}
private int pri = 3;
private static int sta = 1;
protected int pub = 2;
protected Printf prin = new Printf();
protected String str = new String("after");
}
代码中其他都比较好理解,只有三个protected数据不太好理解,我们发现pub和str被赋值为-1与before没有打印出来,很有可能是基类构造函数之后和派生类构造函数的两次打印之前被赋值了(当然也有可能是根本就没有发生这一步),同时print!!!被打印了两次,都是在构造函数打印之前,再加上书中说所有事发生之前将对象的储存空间化为二进制0,是不是可以猜测,对于类对象化为0这一步也是使用相应的构造器完成的????