public class PolymorphismTest {
public void show() {
A a = new B();
B b = new B();
a.show();
b.show();
System.out.println("a.a: " + a.a);
System.out.println("b.a: " + b.a);
}
public class A {
public int a = 5;
public void show() {
System.out.println("A show");
}
}
public class B extends A {
public int a = 6;
public void show() {
System.out.println("B show");
}
}
}
show()函数执行实验结果:
B show
B show
a.a: 5
b.a: 6
为什么这里a.a的值是5而不是6呢?