public class PolymophyTest {
public static void main(String[] args) {
SuperA t = new SubB();
System.out.println("t.a = " + t.a); // t.a = 10;
t.test();; //打印SubB.method()
}
}
class SuperA {
int a = 10;
public void test() {
System.out.println("SuperA.method()");
}
}
class SubB extends SuperA {
int a = 100;
public void test() {
System.out.println("SubB.method()");
}
}
不是说 SuperA t = new SubB(); 这时的t在内存中指向的是SubB, 可为什么会出现取方法和取变量时候的不一样咧? |
|