class A {
int i = 1;
int f() { return i; }
}
class B extends A {
int i = 2;
int @Override f() { return -i; }
}
public class override_test {
public static void main(String args[]) {
B b = new B();
A a = (A) b; // Cast b to an instance of class A.
System.out.println(a.i); // Now refers to A.i; prints 1;
System.out.println(a.f()); // Still refers to B.f(); prints -2;
}
}
我想知道为什么 af() 仍然指的是 Bf() 而 ai 指的是 Ai
提前致谢!