java中从子类对象调用父类方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3797206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
calling parent class method from child class object in java
提问by ranjeet kumar
I have a parent class which have a method, in child class i have override that parent class method . In a third class i make a object of child and by using that object i want call method of parent class.Is it possible to call that parent class method ? If yes then how?
我有一个父类,它有一个方法,在子类中我已经覆盖了父类的方法。在第三个类中,我创建了一个 child 对象,并通过使用该对象来调用父类的方法。是否可以调用该父类的方法?如果是,那么如何?
Please reply
请回复
回答by Erkan Haspulat
If you override a parent method in its child, child objects will always use the overridden version. But; you can use the keyword super
to call the parent method, inside the body of the child method.
如果在其子对象中覆盖父方法,子对象将始终使用被覆盖的版本。但; 您可以使用关键字super
在子方法的主体内调用父方法。
public class PolyTest{
public static void main(String args[]){
new Child().foo();
}
}
class Parent{
public void foo(){
System.out.println("I'm the parent.");
}
}
class Child extends Parent{
@Override
public void foo(){
//super.foo();
System.out.println("I'm the child.");
}
}
This would print:
这将打印:
I'm the child.
我是孩子。
Uncomment the commented line and it would print:
取消注释注释行,它将打印:
I'm the parent.
I'm the child.
我是家长。
我是孩子。
You should look for the concept of Polymorphism.
你应该寻找多态的概念。
回答by Sagar V
Use the keyword superwithin the overridden method in the child class to use the parent class method. You can only use the keyword within the overridden method though. The example below will help.
在子类的重写方法中使用关键字super来使用父类方法。但是,您只能在重写的方法中使用关键字。下面的例子会有所帮助。
public class Parent {
public int add(int m, int n){
return m+n;
}
}
public class Child extends Parent{
public int add(int m,int n,int o){
return super.add(super.add(m, n),0);
}
}
public class SimpleInheritanceTest {
public static void main(String[] a){
Child child = new Child();
child.add(10, 11);
}
}
The add
method in the Child class calls super.add
to reuse the addition logic.
add
Child 类中的方法调用super.add
以重用加法逻辑。
回答by Gabriel ??erbák
First of all, it is a bad design, if you need something like that, it is good idea to refactor, e.g. by renaming the method. Java allows calling of overriden method using the "super" keyword, but only one level up in the hierarchy, I am not sure, maybe Scala and some other JVM languages support it for any level.
首先,这是一个糟糕的设计,如果您需要类似的东西,重构是个好主意,例如通过重命名方法。 Java 允许使用“super”关键字调用覆盖方法,但在层次结构中只有一个级别,我不确定,也许 Scala 和其他一些 JVM 语言支持任何级别的它。
回答by Tony Ennis
Say the hierarchy is C->B->A
with A being the base class.
假设层次结构是C->B->A
A 作为基类。
I think there's more to fixing this than renaming a method. That will workbut is that a fix?
我认为解决这个问题不仅仅是重命名方法。这会起作用,但这是一个修复吗?
One way is to refactor all the functionality common to B and C into D, and let B and C inherit from D: (B,C)->D->A
Now the method in B that was hiding A's implementation from C is specific to B and stays there. This allows C to invoke the method in A without any hokery.
一种方法是将 B 和 C 共有的所有功能重构为 D,并让 B 和 C 从 D 继承: (B,C)->D->A
现在 B 中隐藏 A 对 C 的实现的方法特定于 B 并保持在那里。这允许 C 调用 A 中的方法而无需任何技巧。
回答by Kanagavelu Sugumar
NOTEcalling parent method via super will only work on parent class,
If your parent is interface, and wants to call the default methods then need to add interfaceName before super like IfscName.super.method();
注意通过super调用父方法只对父类有效,如果你的父类是interface,并且想要调用默认方法那么需要在super之前添加interfaceNameIfscName.super.method();
interface Vehicle {
//Non abstract method
public default void printVehicleTypeName() { //default keyword can be used only in interface.
System.out.println("Vehicle");
}
}
class FordFigo extends FordImpl implements Vehicle, Ford {
@Override
public void printVehicleTypeName() {
System.out.println("Figo");
Vehicle.super.printVehicleTypeName();
}
}
Interface name is needed because same default methods can be available in multiple interface name that this class extends. So explicit call to a method is required.
需要接口名称是因为可以在此类扩展的多个接口名称中使用相同的默认方法。因此需要显式调用方法。