简体   繁体   English

如何在Java中使用Child对象调用Parent类的方法

[英]How to call method of Parent class using Child object in Java

Parent Class Method name -> calculateTax Since Child Method name -> calculateTax 父类方法名称-> calculateTax自子方法名称-> calculateTax

class will inherit this method I still want to call parent class calculateTax method 类将继承此方法我仍然想调用父类的calculateTax方法

Child ch = new Child() ch.calculatetax() -> this should call parent class method , how can this be achieved Child ch = new Child()ch.calculatetax()->应该调用父类方法,如何实现

This is not possible, in your case since you are calling calculateTax method with the object of Child class. 在您的情况下,这是不可能的,因为您正在使用Child类的对象来调用calculateTax方法。 The method of child class will override the calculateTax of parent class. 子类的方法将覆盖父类的calculateTax。

But you can call the parent class method by using super keyword inside the child method body like:- 但是您可以通过在子方法主体内使用super关键字来调用父类方法,例如:

super.calculateTax ();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM