【问题标题】:How to call Parent overridden method如何调用父重写方法
【发布时间】:2016-10-12 14:54:40
【问题描述】:

我有两个类 Parent 和 Child。从 Child 类我调用父类重写的方法 (show)。从 Parent 类,我调用另一个方法 (display),但由于调用了 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

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

【问题讨论】:

  • 你在节目中做到了,但没有在展示中......为什么?从显示器调用 super.display()。
  • @AxelH 我想从同一个类的 show() 方法中调用 Parent 类的 display() 方法

标签: java inheritance polymorphism


【解决方案1】:

当您在子对象中覆盖 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 修饰符。

【讨论】:

    【解决方案2】:

    有什么问题:

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

    郑重声明:您真的真的很想将@Override 放在您认为会覆盖某些内容的每个和任何 方法上。经常发生的情况是,您只是假设覆盖某些内容而没有实际执行。 @Override 指示编译器在您犯此类错误时告诉您。

    编辑:请注意 - 在某些情况下,您似乎希望将 show+display 称为“一起”。如果是这样:在您的“接口”上只放置 一个 方法,而不是两个!我的意思是:如果这些方法的想法是一个接一个地运行,那么提供一种有意义的方式来做到这一点。

    换句话说:好的界面让正确的事情变得容易;并且很难做错误的事情。那里有两种方法,并期望其他代码按顺序调用它们,这与该想法相反。它很容易出错;更难把事情做好!

    最后:命名已经指出手头存在一定的设计问题。如:首先“显示”和“显示”之间到底有什么区别?!

    【讨论】:

    • 我想从同一个类的show方法中调用Parent类的显示方法。
    • 德克萨斯州。但我希望父类的显示方法只能从同一类的显示方法中调用
    • 我不得不承认:我不明白你的问题。我的代码示例应该会创建您正在寻找的输出。
    • 上面的例子是继承的缺点,即紧密耦合。但如果它也被覆盖,我坚持调用父方法。
    • 还是不知道你在说什么。由于您需要调用两个超级方法,因此“紧密”耦合开始发挥作用。手头的问题是你的设计;而不是多态性或继承。
    【解决方案3】:
    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() 时,计算机是这样的: 我有对象ChildParent 引用(它可以是Child 引用,并且在这个例子中输出仍然是相同的)。 Child 对象有 show() 打印“显示孩子”,然后调用 Parent 类的 show() 方法。 Parent show() 方法打印“显示父级”,然后调用 display()。由于display()staticParent 只知道它自己的display() 方法,因此会打印“显示父级”。

    虽然这是一个选项,但最好不要使用它,因为它会导致代码混乱和难以阅读。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 2011-02-23
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多