以下代码在编译和运行时将输出作为“alpha subsub”。
SubSubAlpha();
构造函数应该将“subsub”添加到变量s
中,这应该是输出。
为什么输出是“ alpha subsub”?
class Alpha {
static String s = " ";
protected Alpha() {
s += "alpha ";
}
}
public class SubSubAlpha extends Alpha {
private SubSubAlpha() {
s += "subsub ";
}
public static void main(String[] args) {
new SubSubAlpha();
System.out.println(s);
// This prints as " alpha subsub".
//Shouldn't this one be printed as " subsub"
//Who made the call to Alpha(); ?
}
}