我有一个方法,一个抽象的超A级doSomething()
。A
必须实现的子类doSomething()
,但每次子类调用时,也应调用一些通用代码doSomething()
。我知道可以这样实现:
public class A {
public void doSomething() {
// Things that every sub-class should do
}
}
public class B extends A {
public void doSomething() {
super.doSomething();
// Doing class-B-specific stuff here
...
}
}
但是,似乎存在三个问题:
任何想法应如何实施方法?
将调用添加回doSomething()
public class A {
public void doSomething() {
// Things that every sub-class should do
doSomethingMore()
}
}
protected abstract void doSomethingMore()
因此,所有子句都必须通过额外的动作修改doSomethingMore(),但是外部类将调用public doSomething()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句