如何调用父重写方法 [英] How to call Parent overridden method

查看:69
本文介绍了如何调用父重写方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类 Parent 和 Child.From Child 类我正在调用父类覆盖的方法(show).从父类中,我调用另一个方法(display)但是由于调用了 Child 方法,该方法也被覆盖.我想从 show 方法中调用 Parent 方法显示.

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. I want to call the Parent method display from show method.

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();
    }


}

输出:

Show of child 
Show of parent 
Display of child

需要:

Show of child 
Show of parent 
Display of parent

即我想从同一个类的 show() 方法调用父类的 display() 方法

i.e. I want to call display() method of Parent class from show() method of same class

推荐答案

当你在子对象中覆盖 display() 方法时,从子对象调用它会调用被覆盖的方法而不是父的方法,因为它被覆盖了.如果你想在执行一些操作的同时调用父级的方法,那么你需要调用 super.display() 来执行父级的 display() 方法.所以,

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. 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. So,

  1. 如果您只想执行父级的 display() 方法,则不要在特定子级中覆盖它.

  1. If you want to execute only the parent's display() method then don't override it in the specific child.

如果您只想执行孩子的显示,那么现有代码就可以了.

If you want to execute only the child's display then existing code is fine.

如果你想调用父级的 display() 方法并且还想执行一些额外的任务,那么将它们与 super.display() 方法混合使用.

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.

示例

根据我上面的解释,我正在跟随 3 个孩子,

Based on my above explanation I am making follow 3 child,

Child1 很可能是您在这里需要的,因为它会打印父级的 display() 方法,

Child1 is most probably what you need here as it will print the parent's display() method,

public class Child1 extends Parent{

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

    }

Child2 中,我们忽略了 parent 的 display() 方法,我们希望 child2 的对象只执行 child2 的 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

public class Child2 extends Parent{

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

    }

Child3 中,我们需要孩子和父母的 display() 方法

In Child3 we want both child's and parent's display() method

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");
        }

    }

奖励:如果您不希望任何子级覆盖它的 display() 方法,那么在父级的 display() 方法之前添加一个 final 修饰符.

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.

这篇关于如何调用父重写方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆