【发布时间】:2021-10-22 15:21:59
【问题描述】:
我有这样的代码:
interface Contract {
createSomething(); //not common
updateSomething(); //not common
getSomething(); //method who is supposed to be common between all strategies
}
interface Strategy {
createSomething();
updateSomething();
getSomething();
}
Abstract class AbstractStrategy implements Strategy {
@Override
getSomething() {
// the common code
}
}
class strategyA extends AbstractStrategy {
@Override
createSomething() {...}
@Override
updateSomething() {...}
}
class ContractImpl implements Contract {
@Override
createSomething() {
//get the good strategy
//call the strategy.createSomething();
}
@Override
updateSomething() {
//get the good strategy
//call the strategy.updateSomething();
}
@Override
getSomething() {
**Here is the question**
}
}
问题:
- 我怎样才能重写这段代码,这样我就可以调用 getSomething() 方法,而不必实例化一个随机子类,只是用 super 关键字调用它?
【问题讨论】:
-
我很确定你不能,因为这是抽象类的重点。或者您正在寻找一种静态方法,但这是不同的。
标签: java design-patterns abstract-class