在以下代码示例中:
class Parent {
int x =5;
public Integer aMethod(){
System.out.print("Parent.aMthod ");
return x;
}
}
class Child extends Parent {
int x =6;
public Integer aMethod(){
System.out.print("Child.aMthod ");
return x;
}
}
class ZiggyTest2{
public static void main(String[] args){
Parent p = new Child();
Child c = new Child();
System.out.println(p.x + " " + c.x);
System.out.println(p.aMethod() + " \n");
System.out.println(c.aMethod() + " \n");
}
}
和输出:
5 6
Child.aMthod 6
Child.aMthod 6
为什么p.aMethod()px打印6时不打印6?
谢谢
编辑
哎呀,有点错字:问题应该是“为什么 p.aMethod() 在 px 打印 5 时不打印 5” - 谢谢@thinksteep