class Base {
int i = 99;
public void amethod () {
System.out.println (“Base.amethod ”);
}
Base () {
amethod ();
}
}
public class RType extends Base {
int i = -1;
public static void main (String argv []) {
Base b = new RType ();
System.out.println (b.i);
b.amethod ();
}
public void amethod () {
System.out.println (“RType.amethod ”);
}
}输出结果:
RType.amethod
99
RType.amethod为什么不是
Base.amethod
99
RType.amethod
int i = 99;
public void amethod () {
System.out.println (“Base.amethod ”);
}
Base () {
amethod ();
}
}
public class RType extends Base {
int i = -1;
public static void main (String argv []) {
Base b = new RType ();
System.out.println (b.i);
b.amethod ();
}
public void amethod () {
System.out.println (“RType.amethod ”);
}
}输出结果:
RType.amethod
99
RType.amethod为什么不是
Base.amethod
99
RType.amethod
在这种情况下,涉及的任何域,比如i,都会指向Base 类中的值,但是方法的调用将
会指向实际类中的方法而不是引用句柄中的方法。