11

我有一个简单的问题:

为什么 Eclipse 对实现这两个接口大喊大叫?

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

我收到此错误消息:

IPlayerity 无法解析为类型

4

4 回答 4

5

在另一个文件中声明接口。对我来说很明显,顶级类无法实现嵌套在自身内部的接口(尽管我不确定为什么)。

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

于 2011-07-15T18:37:58.503 回答
5

鉴于 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
于 2011-07-15T19:02:20.443 回答
0

我看这两个接口不是默认的java接口吧?因此,如果您收到错误“IPlayerity cannot be resolved to a type.”,请检查您是否导入了正确的包。我应该对此发表评论,但我不能。玩得开心。

于 2011-07-15T18:33:38.257 回答
-1

如果它不能被解析为一种类型,那么它就不能被“看到”。您应该为接口添加一个导入:

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

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

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

于 2011-07-15T18:43:38.020 回答