我試圖定義這個繼承:辛格爾頓+抽象類問題
public abstract class GameAction extends Observable {
protected static GameAction instance = null;
protected GameAction() {
// Exists only to defeat instantiation.
}
//All GameActions are singletons
public static abstract GameAction getInstance();
}
public class ChooseAnswerAction extends GameAction {
protected ChooseAnswerAction() {
// Exists only to defeat instantiation.
}
//All GameActions are singletons
@Override
public static GameAction getInstance() {
if(instance == null) {
instance = new ChooseAnswerAction();
}
return instance;
}
}
的問題是,在第二類中的方法getInstance()
沒有找到他的父親同一個,爲此它要求我刪除@Override
。
同樣在父類中,我得到了以下錯誤:
The abstract method getInstance in type GameAction can only set a visibility modifier, one of public or protected
我能解決這個錯誤的唯一方法是取出static修飾符,但我需要它...
感謝對你的時間!
靜態方法和字段不是繼承的,它們屬於它們已定義的類,這就是爲什麼您無法覆蓋它。出於同樣的原因,靜態方法不能是抽象的。 – m0skit0 2015-02-06 18:10:44