简体   繁体   English

如何调用父级重写方法

[英]How to call Parent overridden method

I am having two classes Parent and Child.From Child class I am calling parent overridden method (show).From Parent class, I am calling another method(display) but that method is also overridden due to which Child method is called. 我有两个类Parent和Child.From Child类,我正在调用父级重写方法(显示)。从Parent类,我正在调用另一个方法(显示),但是由于调用了Child方法,该方法也被重写了。 I want to call the Parent method display from show method. 我想从show方法调用父方法显示。

public class Parent {


    public void show()
    {
        System.out.println("Show of parent ");
        this.display();
    }

    public void display()
    {
        System.out.println("Display of parent");
    }

}

public class Child extends Parent{

    public void show()
    {
        System.out.println("Show of child ");
        super.show();
    }

    public void display()
    {
        System.out.println("Display of child");
    }

    public static void main(String[] args) {
        Parent obj = new Child();
        obj.show();
    }


}

Output : 输出:

Show of child 
Show of parent 
Display of child

Need : 需要:

Show of child 
Show of parent 
Display of parent

ie I want to call display() method of Parent class from show() method of same class 即我想从同一个类的show()方法调用父类的display()方法

Whats wrong with: 有什么问题:

public void show()
{
    System.out.println("Show of child ");
    super.show();
    super.display();
}

And for the record: you really really really want to put @Override on each and any method that you think overrides something. 记录:您真的很想在每个您认为覆盖某些方法的方法上都使用 @Override。 It happens far to often that you only assume to override something without actually doing it. 经常发生的情况是,您仅假定要覆盖某些内容而没有实际执行。 @Override instructs the compiler to tell you when you make such mistakes. @Override指示编译器在您犯此类错误时告诉您。

EDIT: and please note - it seems that you want that show+display are called "together" in certain situations. 编辑:并且请注意-在某些情况下,您似乎希望将show + display称为“ together”。 If so: put only one method on your "interface", not two! 如果是这样:在您的“接口”上仅放置一种方法,而不是两种! What I mean is: if the idea of those methods is to run one after the other, then provide a meaningful way to do that. 我的意思是:如果这些方法的想法是一个接一个地运行,那么请提供一种有意义的方式来实现。

In other words: good interfaces make it easy do to the right thing; 换句话说:良好的界面可以使正确的事情变得容易; and hard to do the wrong thing. 很难做错事。 Having two methods there, and expecting other code to call them in sequence achieves the opposite of that idea. 在那儿有两个方法,并期望其他代码按顺序调用它们,这与该想法相反。 It makes it easy to get things wrong; 它很容易使事情出错。 and harder to get things right! 更难解决问题!

As finally: already the naming points out that there is a certain design problem at hand. 最后:命名已经指出手头上存在某些设计问题。 As in: what exactly is the difference between "showing" and "displaying" in the first place?! 如:首先显示“显示”和“显示”之间到底有什么区别?

public class Parent {


    public void show()
    {
        System.out.println("Show of parent ");
        display();
    }

    public static void display()
    {
        System.out.println("Display of parent");
    }

}

public class Child extends Parent{

    public void show()
    {
        System.out.println("Show of child ");
        super.show();
    }

    public static void display()
    {
        System.out.println("Display of child");
    }

    public static void main(String[] args) 
    {
        Parent obj = new Child();
        obj.show();
    }

}

Explaination: 说明:

This has to do with hiding methods. 这与隐藏方法有关。 Rules for hiding a method are the same rules for overriding with addtion of static keyword. 隐藏方法的规则与通过添加static关键字进行覆盖的规则相同。 With static keyword, you are hiding display() method. 使用static关键字,您将隐藏display()方法。 So when obj calls method show() , computer goes like this: I have object Child with Parent reference(it can be Child reference and output will still be the same in this example). 因此,当obj调用方法show() ,计算机将如下所示:我有对象ChildParent引用(可以是Child引用,在此示例中输出仍然相同)。 Child object has show() which prints "Show of child" and then calls Parent class's show() method. Child对象具有show() ,它打印“ Show of child”,然后调用Parent类的show()方法。 Parent show() method prints "Show of parent" and then calls display() . Parent show()方法打印“父显示”,然后调用display() Since display() is static , Parent knows only about its own display() method and therefore "Display of parent" is printed. 由于display()static ,因此Parent仅了解其自己的display()方法,因此将打印“ parent of display”。

Although, this is an option maybe it is best not to use this since it can lead to confusing and hard-to-read code. 虽然,这是一个选择,但最好不要使用它,因为它可能导致混乱和难以阅读的代码。

When you override the display() method in the child, calling it from child object will call the overridden method instead of the parent's because it's overridden. 当您在子对象中覆盖display()方法时,从子对象中调用该方法将调用被覆盖的方法,而不是父方法,因为它被覆盖了。 If you want to perform some operation and at the same time call the parent's method as well then you need to call super.display() to execute the parent's display() method. 如果要执行某些操作并同时调用父对象的方法,则需要调用super.display()来执行父对象的display()方法。 So, 所以,

  1. If you want to execute only the parent's display() method then don't override it in the specific child. 如果只想执行父级的display()方法,则不要在特定的子级中覆盖它。

  2. If you want to execute only the child's display then existing code is fine. 如果您只想执行孩子的显示,那么现有的代码就可以了。

  3. If you want to call the parent's display() method and also want to perform some additional tasks then mix them with super.display() method. 如果要调用父级的display()方法并且还想执行一些其他任务,则将它们与super.display()方法混合。

Example

Based on my above explanation I am making follow 3 child, 根据我的上述解释,我正在追踪3个孩子,

Child1 is most probably what you need here as it will print the parent's display() method, Child1很可能是您在这里需要的,因为它将打印父级的display()方法,

public class Child1 extends Parent{

     // Simply deleting this method will produce exactly the same result
        public void display()
        {
            super.display();
        }

    }

In Child2 we are ignoring parent's display() method and we want the child2's object to perform only the commands written in child2's display() method Child2中,我们忽略了父级的display()方法,我们希望child2的对象仅执行在child2的display()方法中编写的命令。

public class Child2 extends Parent{

        public void display()
        {
            System.out.println("Display of child");
        }

    }

In Child3 we want both child's and parent's display() method Child3中,我们需要孩子和父母的display()方法

public class Child3 extends Parent{

        public void display()
        {
             System.out.println("Display of child before parent's display");
             super.display();
             System.out.println("Display of child after parent's display");
        }

    }

Bonus: if you don't want any child to override it's display() method then add a final modifier before the display() method of the parent. 奖励:如果您不希望任何子项覆盖它的display()方法,则在父项的display()方法之前添加一个final修饰符。

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

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