这是一道类多态的向上转型题。
Base b = new Derived();
向上转型:父类只能调用父类方法或者子类覆写后的方法,而子类中的单独方法则是无法调用的。
因此:
调用的顺序是:(1)、(2)、(3)、(4)、(5)、(6)、(7) ; 得到的结果是:ABDC
class Base
{ public void methodOne() // (3)
{
System.out.print("A");
methodTwo(); //执行到这里的时候调用子类的覆盖方法 (4)
} public void methodTwo() // (7)
{
System.out.print("B");
}
}
class Derived extends Base { public void methodOne() // (1)
{ super.methodOne(); // (2)
System.out.print("C");
} public void methodTwo() //该方法为重写了父类的方法 (5)
{ super.methodTwo(); //(6)
System.out.print("D");
}
}