两个或更多接口可以使用相同的方法吗?

Nic*_*era 4 java oop interface

我正在用Java做一个简单的游戏,我有这个疑问.

假设每个游戏角色都有一个界面

public interface Entity{
   Vector2 getPosition();
   /* More methods...*/
}
Run Code Online (Sandbox Code Playgroud)

然后我想创建一个名为Automata的接口,由每个使用AI东西的类实现(这可能是Entity的一个特例,但我认为它因为可重用性而单独考虑)

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)... */
}
Run Code Online (Sandbox Code Playgroud)

我认为这促进了模块化,因为每个接口都描述了它的方法,而不用担心其他接口的存在,但是当我写这篇文章时,我觉得我在重复自己,所以这两个(或可能更多)接口使用相同的方法什么坏事?

Nic*_*s K 6

如果有两个接口之间共同的东西,那么也许你可以定义一个父接口,然后EntityAutomata可以扩展它.

我在下面说明一下:

interface AI {
    Vector2 getPosition();
}

interface Entity extends AI { }
interface Automata extends AI { }
Run Code Online (Sandbox Code Playgroud)

这样,作为AI一部分的任何其他接口都不需要显式添加另一个方法,而只需要扩展 AI