遇到一道多态引用的选择题,唤醒我痛苦的回忆。
一,多态引用问题
public class Base {
public void methodOne(){
System.out.println("A");//4,输出A
methodTwo();//5,调用子类的methodTwo()方法
}
public void methodTwo(){
System.out.println("B");//7,输出B,方法结束返回
}
}
public class Derived extends Base {
public void methodOne(){
super.methodOne();//3,在父子类中,如果方法前没有声明super的话,默认都是调用子类的方法,此处调用父类的methodOne()方法
System.out.println("C");//9,父类的methodA()方法执行完毕,返回,输出C
}
public void methodTwo(){
super.methodTwo();//6,调用父类的methodTwo()方法
System.out.println("D");//8,父类methodTwo()方法执行完毕,输出D
}
public static void main(String[] args) {
Base b = new Derived();//1,实例化对象
b.methodOne();//2,调用Derived类的methodOne()方法
}
}
最终结果:ABDC