大家说这道程序运行结果输出什么?
class Parent {
String s = "Parent ";
public Parent(){
method();
}
public void method(){
System.out.println(s);
}
}
class Child extends Parent {
String s = "Child ";
public Child(){
}
public void method(){
System.out.println(s);
}
}
public class ObjectTest {
public static void main(String[] args) {
Child child = new Child();
}
}
------解决方案--------------------父类的构造方法调用子类的method,但这个时候子类的String并没有初始化,所以打印null