java子类调用父类的方法中包含子类重写的实例方法
#看题目是不是很绕,这个我也不知道怎么才能更简单的表达了
#先看代码:
publicclassCommon{
publicstaticvoidmain(String[]args){
Subsub=newSub();
sub.testSub();
}
}
classParent{
protectedbooleantest(){
thrownewRuntimeException();
}
protectedvoidtestParent(){
if(test()){
System.out.println(this.getClass().getName());
}
}
}
classSubextendsParent{
publicvoidtestSub(){
super.testParent();
}
@Override
publicbooleantest(){
returntrue;
}
}
#上面的代码的输出结果是:
com.qq.demo.common.Sub
#大致流程是main中调用子类的testSub()方法--testSub()方法中调用父类的testParent()方法--testParent()方法调用test()方法;需要注意的是test()方法在父类和子类中都存在,子类重写了父类的test()方法;
以上就是本次介绍的全部知识点内容,感谢大家的阅读和对毛票票的支持。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短