为什么跑步
Larry var3 = new Jerry();
var3.method3();
输出错误,而不是
拉里1
哈里1
玛丽3
我认为这可能与对象的创建方式有关。但这只是我的猜测。
代码如下:
public class Harry {
public void method1() {
System.out.println("harry 1");
}
public void method2() {
method1();
System.out.println("harry 2");
}
}
public class Larry extends Harry {
public void method1() {
System.out.println("larry 1");
super.method1();
}
}
public class Mary extends Larry {
public void method2() {
System.out.println("mary 2");
}
public void method3() {
super.method1();
System.out.println("mary 3");
}
}
public class Jerry extends Mary {
public void method2() {
super.method2();
System.out.println("jerry 2");
}
}