扩展内部接口?

时间:2011-07-15 18:08:17

标签: java inheritance interface implements

我有一个简单的问题:

为什么Eclipse会为实现这两个接口而尖叫?

public abstract class Gateway implements IPlayerity, IItemity {
    public interface IPlayerity { ... }
    public interface IItemity { ... }
    // I...ity
}

我收到此错误消息:

  

IPlayerity无法解析为类型

4 个答案:

答案 0 :(得分:5)

在另一个文件中声明接口。对我来说很明显,顶级类不能实现嵌套在其自身内的接口(虽然我不知道为什么)。

如果要将接口保留在同一个文件中,则必须将修饰符从public更改为default,并在类定义后声明它们。

答案 1 :(得分:4)

你有一个循环依赖,无法解决JLS的工作方式(虽然我不确定JLS在哪里记录)。

接口IPlayerity和IItemity对NestedInterfaces类头定义不可见,因为它们位于其中。我可以通过将程序更改为

来解决此问题
public class NestedInterfaces implements 
      NestedInterfaces.IPlayerity, NestedInterfaces.IItemity 
{
    public interface IPlayerity {}
    public interface IItemity {}
}

然后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

答案 2 :(得分:0)

我看到这两个界面不是默认的java界面,对吧?因此,如果您收到错误“IPlayerity无法解析为某种类型。”,请检查您是否导入了正确的包。我应该对此发表评论,但我不能。玩得开心。

答案 3 :(得分:-1)

如果无法将其解析为类型,则无法“看到”。您应该为界面添加导入:

import package.Gateway.IPlayerity
import package.Gateway.IItemity

仍然,我有一种感觉,然后编译器会抛出一些其他奇怪的循环错误..

如果在另一个文件中定义接口,最好。

相关问题