public class Base {
private String baseName = "Base";
public Base() {
callName();
}
public void callName() {
System.out.println(baseName);
}
static class Sub extends Base {
private String baseName = "Sub";
public void callName() {
System.out.println(baseName);
}
}
public static void main(String[] args) {
Base b = new Sub();
System.out.println(b == null);
}
private String baseName = "Base";
public Base() {
callName();
}
public void callName() {
System.out.println(baseName);
}
static class Sub extends Base {
private String baseName = "Sub";
public void callName() {
System.out.println(baseName);
}
}
public static void main(String[] args) {
Base b = new Sub();
System.out.println(b == null);
}
}
问:输出什么?
答案:null
Sub 初始化时候调用了父类的构造器,父类构造器执行时,子类属性还未初始化,所以输出null