本文介绍了Java - 强制实现已实现的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个课程有问题.它们被命名为:GameScene、StageScene、StageOne.我的问题是我想在 StageScene 中实现 initialize,但仍然强制 StageOne 实现它,这样每当有人使用 StageOne 对象 (stageOne.initialize()) 时,都会为 StageScene 和 StageOne 运行 initialize.有谁知道如何做到这一点?
I have three classes which I have a problem with. They are named: GameScene, StageScene, StageOne. My problem is that I want to implement initialize in StageScene, but still force StageOne to implement it, so that whenever someone uses a StageOne object (stageOne.initialize()), initialize would be run for both StageScene and StageOne. Anyone know how this could be done?
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
}
}
推荐答案
您可以使用包装器来实现:
you can do it with a wrapper:
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
}
}
这篇关于Java - 强制实现已实现的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!