成都创新互联网站制作重庆分公司

java子类调用父类的方法中包含子类重写的实例方法

# 看题目是不是很绕,这个我也不知道怎么才能更简单的表达了

创新互联建站主要从事网站建设、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务滨州,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

# 先看代码:

public class Common {

  public static void main(String[] args) {
    Sub sub = new Sub();
    sub.testSub();
  }
}

class Parent {

  protected boolean test() {
    throw new RuntimeException();
  }

  protected void testParent() {
    if (test()) {
      System.out.println(this.getClass().getName());
    }
  }

}

class Sub extends Parent {

  public void testSub() {
    super.testParent();
  }

  @Override
  public boolean test() {
    return true;
  }
}

# 上面的代码的输出结果是:

com.qq.demo.common.Sub

# 大致流程是 main 中调用子类的 testSub()方法 -- testSub()方法中调用父类的testParent() 方法 -- testParent() 方法调用test()方法;需要注意的是test()方法在父类和子类中都存在,子类重写了父类的test()方法;

以上就是本次介绍的全部知识点内容,感谢大家的阅读和对创新互联的支持。


本文标题:java子类调用父类的方法中包含子类重写的实例方法
URL链接:http://cxhlcq.com/article/iihoch.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部