ima*_*ake 10 java inheritance interface implements
我有一个简单的问题:
为什么Eclipse会为实现这两个接口而尖叫?
public abstract class Gateway implements IPlayerity, IItemity {
public interface IPlayerity { ... }
public interface IItemity { ... }
// I...ity
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息:
IPlayerity无法解析为某种类型
在另一个文件中声明接口.对我来说很明显,顶级类不能实现嵌套在其自身内的接口(虽然我不知道为什么).
如果要将接口保留在同一文件中,则必须将修饰符从public更改为default,并在类定义后声明它们.
考虑到 JLS 的工作方式,您有一个无法解决的循环依赖(尽管我不确定在 JLS 中的哪个位置记录了这一点)。
接口 IPlayerity 和 IItemity 对 NestedInterfaces 类标头定义不可见,因为它们在其中。我可以通过将您的程序更改为
public class NestedInterfaces implements
NestedInterfaces.IPlayerity, NestedInterfaces.IItemity
{
public interface IPlayerity {}
public interface IItemity {}
}
Run Code Online (Sandbox Code Playgroud)
但是后来 Eclipse 给了我这个错误,这更清楚:
Multiple markers at this line
- Cycle detected: the type NestedInterfaces cannot extend/implement itself or one of its own member types
- Cycle detected: the type NestedInterfaces cannot extend/implement itself or one of its own member types
Run Code Online (Sandbox Code Playgroud)