我有三个课我遇到问题。它们被命名为:GameScene,StageScene,StageOne。我的问题是我想在StageScene中实现初始化,但仍然强制StageOne实现它,这样每当有人使用StageOne对象(stageOne.initialize())时,都会为StageScene和StageOne运行初始化。任何人都知道如何做到这一点?
public abstract class GameScene
{
public abstract void initialize();
}
public abstract class StageScene extends GameScene
{
public abstract void initialize()
{
//Some code
}
}
public class StageOne extends StageScene
{
public void initialize()
{
//Some code
}
}
答案 0 :(得分:7)
你可以用包装器做到这一点:
public abstract class StageScene extends GameScene
{
final public void initialize()
{
//your initialization
subInitialize();
}
protected abstract void subInitialize();
}
在儿童班:
public class StageOne extends StageScene
{
public void subInitialize()
{
//Some code
}
}
答案 1 :(得分:1)
你可以把它分成两个单独的方法
public abstract class GameScene
{
public abstract void initializeScene();
public abstract void initializeStage();
}
public abstract class StageScene extends GameScene
{
public void initializeScene()
{
//Some code
}
}
public class StageOne extends StageScene
{
public void initializeStage()
{
//Some code
}
}