重写一个方法,让所有父级都在该方法中工作?
我有一个 B 类,它从 A 类扩展,B 类覆盖 A 类的方法:
public class ClassB extends ClassA {
@Overrides
protected void DoSomething() {
super.DoSomething();
// Some ClassB code here, called WorkB
}
}
之后,我创建一个 B 类对象,除了 DoSomething() 中 A 的版本之外,我还需要做一些额外的事情:
ClassB objB = new ClassB() {
@Overrides
protected void DoSomething() {
// I need to do something more here
}
}
我需要知道我是否可以完成所有 ClassA
工作、ClassB
工作,然后在此方法中为 objB 添加一些其他代码?我有一个解决方案:在ClassB
中创建一个public方法
,它执行WorkB
,然后在objB
中,只需调用的方法。但我想知道是否有另一种方法(无论我的解决方案更糟还是更好!)。
编辑:我将总结这个问题,以便您可以轻松理解它:
Class A
的doSomething
方法执行名为 WorksA 的操作。Class B
覆盖doSomething
,调用 super.doSomething() 和一些额外的代码,这意味着它执行WorksA
和一个名为WorksB
的额外代码。- 对象
objB
doSomething()
方法必须执行WorksA
、WorksB
以及另一个名为WorksBExtra
的额外操作。
就这样。
I have a class B, that extends from class A, class B overrides a class A's method:
public class ClassB extends ClassA {
@Overrides
protected void DoSomething() {
super.DoSomething();
// Some ClassB code here, called WorkB
}
}
After that, I create a class B object, and I need do something extra in addition to what A's version in DoSomething():
ClassB objB = new ClassB() {
@Overrides
protected void DoSomething() {
// I need to do something more here
}
}
I need to know if I can do ALL of ClassA
works, ClassB
works, then add some other code for objB in this method? I have a solution: create a public method
in ClassB
, that do WorkB
, then in objB
, just call the method. But I want to know if there is/are another way (no matter worse or better my solution!).
EDIT: I will summarise the question so you can easily understand it:
Class A
'sdoSomething
method does something called WorksA.Class B
overridesdoSomething
, call super.doSomething() and some extra code, which mean it doesWorksA
and an extra calledWorksB
.- Object
objB
doSomething()
method has to doWorksA
,WorksB
and another extra calledWorksBExtra
.
That's all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,只要先调用
super.doSomething()
,它就会抛出 A 类,然后是 B 类。之后你就可以做特定的事情了。}
}
输出 A、B、新 B
Yes just call
super.doSomething()
first and it will throw up until Class A and then Class B. After that you can do specific stuff.}
}
Outputs A, B, new B
您的对象是 ClassB 匿名子类的实例,您可以以完全相同的方式进行重写:
此代码将首先调用
DoSomething()
的ClassB
版本,其中调用ClassA
版本。因此,最终效果是首先执行ClassA
内容,然后执行ClassB
内容,最后执行特定于匿名子类的内容。我认为你的注释有误。它是没有“s”的
@Override
。可能只是一个错字。另请注意,您的
DoSomething()
是相当非标准的命名。您最好按照 Java 中通常命名方法的方式将其称为doSomething()
。Your object is an instance of an anonymous subclass of ClassB, and you can override in exactly the same way:
This code will first call the
ClassB
version ofDoSomething()
, which calls theClassA
version. So the net effect is to first do theClassA
stuff, then theClassB
stuff, then the stuff specific to the anonymous subclass.I think you had the annotation wrong. It's
@Override
with no "s". Probably just a typo.Also note that your
DoSomething()
is rather non-standard naming. You'd be better off calling itdoSomething()
in accordance with the way methods are usually named in Java.