这个我之前试过了,就是我想说的意思是,我明明都向下转型了,这时调用的i难道不应该是子类里的i吗?
举个例子:
class Fu
{
int i = 3;
}
class Zi extends Fu
{
public static void main(String[] args)
{
int i = 5;
Fu f = new Zi();
System.out.println(f.i); // i == 3;
Zi z = (Zi)f;
System.out.println(z.i); // i == 3;
}
}
-------------------------------------------------------
class Fu
{
int i = 3;
}
class Zi extends Fu
{
int i = 5;
public static void main(String[] args)
{
Fu f = new Zi();
System.out.println(f.i); // i == 3;
Zi z = (Zi)f;
System.out.println(z.i); // i == 5;
}
}
为什么这两个最后的i的值会不同呢?