我正在用Java做一个简单的游戏,我对此表示怀疑。
假设每个游戏角色都有一个接口
public interface Entity{
Vector2 getPosition();
/* More methods...*/
}然后,我想创建一个名为Automata的接口,它由每个使用AI组件的类实现(这可能是实体的特例,但由于可重用性,我认为它是分开的)。
public interface Automata{
Vector2 getPosition(); // The AI stuff needs to know this
/* More methods needed for AI (some may also be the same as Entity)... */
}我认为这促进了模块化,因为每个接口都描述自己的方法,而不必担心其他接口的存在,但在编写这篇文章时,我觉得我在重复自己,那么,使用相同方法的这两个(或者更多)接口会有什么不好的地方吗?
发布于 2018-10-13 17:43:10
如果两个接口之间有一些共同之处,那么也许您可以定义一个父接口,然后Entity和Automata可以扩展它。
让我在下面说明一下:
interface AI {
Vector2 getPosition();
}
interface Entity extends AI { }
interface Automata extends AI { }这样,作为人工智能一部分的任何其他接口都不需要显式地添加另一个方法,而只需要扩展AI。
https://stackoverflow.com/questions/52795580
复制相似问题