[求助]一道关于override和继承的题目
class X
{
int i;
X()
{
System.out.println(i);
add(1);
System.out.println(i);
}
void add(int v)
{
System.out.println("The add of X is called.");
i = v + i;
}
void print(){System.out.println(i);}
}
class Y extends X
{
Y()
{
System.out.println(i);
add(2);
System.out.println(i);
}
void add(int v)
{
System.out.println("The add of Y is called.");
i = i+ v*2;
}
}
public class override
{
public static void main(String[] args)
{
boogo(new Y());
}
static void boogo( X a)
{
a.add(8);
a.print();
}
}
/**输出结果为
* 0
* The add of Y is called.
* 2
* 2
* The add of Y is called.
* 6
* The add of Y is called.
* 22
**/
可是为什么不是
/**输出结果为
* 0
* The add of Y is called.
* 4 //这里不是调用Y中add的i = i + v * 2么?那就应该是4了啊
* 4
* The add of Y is called.
* 8
* The add of Y is called.
* 24
**/