java 如何覆盖方法来调用超类的超类方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/726599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:31:38  来源:igfitidea点击:

How to override method to invoke superclass' superclass method?

javaoopclass-design

提问by Steve Reed

Part of me thinks that this shouldn't be possible (even if it is), but I'll ask anyway.

我的一部分认为这不应该是可能的(即使是),但无论如何我都会问。

Given the following class hierarchy (Grandparentand Parentare from a 3rd party and thus, not under my control), how would I override myMethod()in Childsuch that it bypasses the overridden implementation in Parentand invokes the one in Grandparent?

鉴于以下类层次结构(Grandparent并且Parent来自第 3 方,因此不在我的控制之下),我将如何覆盖myMethod()inChild使其绕过被覆盖的实现 inParent并调用 in 的实现Grandparent

class Grandparent {
   public void myMethod() {
      // do stuff
   }
}

class Parent extends Grandparent {
   @Override public void myMethod() {
      super.myMethod();
      // do something else
   }
}

class Child extends Parent {
   @Override public void myMethod() {
      // ??? I want to *only* do what Grandparent did here
   }
}

Pretend that the Parentclass provides a lot of other helpful behavior and is a crucial element of Child's hierarchy (in other words, I'm not looking for "make Childa subclass of Grandparent".

假设Parent该类提供了许多其他有用的行为,并且是Child的层次结构的关键元素(换句话说,我不是在寻找“制作”Child的子类Grandparent

回答by Seb

The idea behind inheritanceis that each class defines their methods how they need, so you don't need to be inspecting any code.

继承背后的想法是每个类都定义了他们需要的方法,所以你不需要检查任何代码。

It seems like you're subclassing here just to re-use code, and that's not the idea of subclassing.

看起来你在这里子类化只是为了重用代码,这不是子类化的想法。

Maybe you should have a helper member to do some of the tasks you need, instead of subclassing, and have both "Child" and "Parent" classes extend "Grandparent".

也许你应该有一个助手成员来完成你需要的一些任务,而不是子类化,并且让“子”和“父”类都扩展“祖父”。

The main question you need to ask yourself is: "Is Child really a descendant of Parent, Grandparent or neiter?" In other words, for every instance of Child, can I say it's a Parent?

您需要问自己的主要问题是:“Child 真的是 Parent、Grandparent 或 neiter 的后代吗?” 换句话说,对于 Child 的每个实例,我可以说它是 Parent吗?

If the answer is no, then you're subclassing wrongly: inheritance is supposed to mean something, not just code re-use (i.e. Ford IS ALSOa Car, not just "Ford" uses"Car" methods).

如果答案是否定的,那么你就错子类:继承应该意味着什么,不仅仅是代码复用(即福特IS ALSO一车,不只是“福特”用途“驾驶”的方法)。

回答by BlairHippo

Assuming that I couldn't touch the code in Parent or Grandparent and assuming that I'm not, as Seb suggested (and as Steve apparently agreed) simply misusing inheritance entirely:

假设我无法触及 Parent 或 Grandparent 中的代码,并假设我不是,正如 Seb 建议的那样(并且史蒂夫显然同意)只是完全滥用继承:

I'd create a local instance of a Grandfather object (or a local class extending Grandfather, if it's abstract) and access its interpretation of myMethod() directly. Of course, depending on how much state information myMethod() is supposed to read and/or manipulate, the amount of work involved could be anything from "easy" to "excruciating".

我会创建一个祖父对象的本地实例(或扩展祖父的本地类,如果它是抽象的)并直接访问它对 myMethod() 的解释。当然,根据 myMethod() 应该读取和/或操作的状态信息量,所涉及的工作量可能从“容易”到“难以忍受”。

It's an ugly solution, and, depending on how much state information is accessed, could be brittle as hell. But if Grandfather is reliably stable and/or myMethod() is fairly self-contained, it could work. The devil is in the details, as always.

这是一个丑陋的解决方案,并且取决于访问了多少状态信息,可能会非常脆弱。但是,如果 Grandfather 可靠稳定和/或 myMethod() 相当独立,它就可以工作。魔鬼在细节中,一如既往。

I definitely agree with Seb that this is re-use, not inheritance. But, hey. Re-use is often a Good Thing.

我绝对同意 Seb 的观点,这是重用,而不是继承。但是,嘿。重复使用通常是一件好事。

回答by Mehrdad Afshari

Not possible.

不可能。

I would create a finalhelper method in grandparent instead. And have this method (which is overridden) call that helper.

我会final在祖父母中创建一个辅助方法。并让此方法(已被覆盖)调用该帮助程序。

class Grandparent {
   public final void myHelperMethod() {
      // do stuff
   }
   public void myMethod() {
      myHelperMethod();
   }
}

class Parent extends Grandparent {
   @Override public void myMethod() {
      super.myMethod();
      // do something else
   }
}

class Child extends Parent {
   @Override public void myMethod() {
      // ??? I want to *only* do what Grandparent did here
      myHelperMethod();
   }
}

回答by Patrick McDonald

Do you have control of the Parent class?

您是否可以控制 Parent 类?

If so, could you add a method (myNewMethod) to the Parent that calls myMethod on Grandparent, and call myNewMethod from Child?

如果是这样,您是否可以向在祖父母上调用 myMethod 的父级添加一个方法(myNewMethod),并从子级调用 myNewMethod?

(I'm not a Java person, so don't know if you can only call a method in a superclass from an override of that method in a subclass)

(我不是Java人,所以不知道您是否只能从子类中该方法的覆盖中调用超类中的方法)

class Grandparent {
   public void myMethod() {
      myHelperMethod();
   }
}

class Parent extends Grandparent {
   @Override public void myMethod() {
      super.myMethod();
      // do something else
   }
   public final void myNewMethod() {
      super.myMethod();
   }
}

class Child extends Parent {
   @Override public void myMethod() {
      // ??? I want to *only* do what Grandparent did here
      myNewMethod();
   }
}