简体   繁体   English

java中从子类对象调用父类方法

[英]calling parent class method from child class object in java

I have a parent class which has a method, in the child class I override that parent class's method.我有一个具有方法的父类,在子类中我覆盖了该父类的方法。 In a third class I make an object of child and by using that object I want call the method of parent class.在第三个类中,我创建了一个 child 对象,并通过使用该对象来调用父类的方法。 Is it possible to call that parent class method ?是否可以调用该父类方法? If yes, then how?如果是,那么如何?

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.你应该寻找多态的概念。

Use the keyword super within the overridden method in the child class to use the parent class method.在子类的重写方法中使用关键字super来使用父类方法。 You can only use the keyword within the overridden method though.但是,您只能在重写的方法中使用关键字。 The example below will help.下面的例子会有所帮助。

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. Child 类中的add方法调用super.add来重用加法逻辑。

First of all, it is a bad design, if you need something like that, it is good idea to refactor, eg 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 语言支持任何级别。

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 work but 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.一种方法是将 B 和 C 共有的所有功能重构为 D,并让 B 和 C 从 D 继承: (B,C)->D->A现在 B 中隐藏 A 对 C 的实现的方法是特定的到 B 并留在那里。 This allows C to invoke the method in A without any hokery.这允许 C 调用 A 中的方法而无需任何技巧。

NOTE calling 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之前添加interfaceName,比如IfscName.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.因此需要显式调用方法。

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

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