如何调用Parent重写方法

时间:2016-10-12 14:54:40

标签: java inheritance polymorphism

我有两个类Parent和Child.From Child类我调用parent overridden方法(show).From Parent类,我调用另一个方法(显示)但是该方法也被覆盖,因为调用了哪个Child方法。 我想从show方法中调用Parent方法显示。

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

即。 我想从同一类的display()方法

中调用父类的show()方法

3 个答案:

答案 0 :(得分:2)

错误:

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

并且为了记录:你真的真的想把@Override放在你认为会覆盖的每个和任何方法上。通常情况下,您只需假设覆盖某些内容而不实际执行此操作。 @Override指示编译器在你犯这样的错误时告诉你。

编辑:请注意 - 似乎您希望该节目+显示被称为"在一起"在某些情况下。如果是这样的话:只在你的"接口"上放置一个方法,而不是两个!我的意思是:如果这些方法的想法是一个接一个地运行,那么提供一个有意义的方法来做到这一点。

换句话说:良好的界面可以轻松完成正确的事物;并且很难做错误的事情。在那里有两个方法,并期望其他代码按顺序调用它们实现了与该想法相反的方法。它容易弄错;并且更难把事情搞定!

最后:已经命名指出手头有一定的设计问题。如:"显示"之间的区别究竟是什么?和"显示"首先是什么?!

答案 1 :(得分:0)

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

}

说明:

这与隐藏方法有关。隐藏方法的规则与添加static关键字进行覆盖的规则相同。使用static关键字,您可以隐藏display()方法。 因此,当obj调用方法show()时,计算机将如下所示: 我的对象Child带有Parent引用(它可以是Child引用,在此示例中输出仍然相同)。 Child对象具有show(),该对象打印“ Show of child”,然后调用Parent类的show()方法。 Parent show()方法将显示“显示父项”,然后调用display()。由于display()staticParent仅了解其自己的display()方法,因此将打印“显示父项”。

尽管,这是一个选择,也许最好不要使用它,因为它会导致混乱和难以阅读的代码。

答案 2 :(得分:0)

当您在子对象中覆盖display()方法时,从子对象中调用该方法将调用被覆盖的方法,而不是父方法,因为它被覆盖了。如果要执行某些操作并同时调用父对象的方法,则需要调用super.display()来执行父对象的display()方法。所以,

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

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

  3. 如果您要调用父级的display()方法并且还想执行一些其他任务,则可以将它们与super.display()方法混合。

示例

根据我的上述解释,我正在追踪3个孩子,

Child1 很可能是您所需要的,因为它将打印父级的display()方法,

public class Child1 extends Parent{

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

    }

Child2 中,我们忽略了父级的display()方法,我们希望child2的对象仅执行在child2的display()方法中编写的命令

public class Child2 extends Parent{

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

    }

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

    }

奖金:如果您不希望任何子项覆盖它的display()方法,则在父项的display()方法之前添加一个final修饰符。