scjp 最经典的问题,还是不明白,大家看看!!
class Base {
int i = 99;
public void amethod () {
System.out.println (“Base.amethod ()”);
}
}
public class RType extends Base {
int i = -1;
public static void main (String argv []) {
Base b = new RType (); //<= Note the type
System.out.println (b.i);
b.amethod ();
}
public void amethod () {
System.out.println (“RType.amethod ()”);
}
}
結果:
99
RType.amethod ()
提示:
注意,b 引用的类型是Base,但是实际的类型是类RType。对amethod 的调用将启动
RType 中的版本,但是b.i 输出的调用将引用Base 类中的域i。
为什么是99呢?? 搞不懂!!!