Extending an inner interface?

南楼画角 提交于 2019-12-08 15:40:30

问题


I got a simple question:

Why does Eclipse scream about implementing these two interfaces?

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

I get this error message:

IPlayerity cannot be resolved to a type


回答1:


You have a cyclic dependency that can't be resolved given the way the JLS works (although I'm not sure where in the JLS this is documented).

The interfaces IPlayerity and IItemity are not visible to the NestedInterfaces class header definition, since they are inside it. I can fix this by changing your program to

public class NestedInterfaces implements 
      NestedInterfaces.IPlayerity, NestedInterfaces.IItemity 
{
    public interface IPlayerity {}
    public interface IItemity {}
}

but then Eclipse gives me this error, which is much more clear:

 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:


Declare the interfaces in another file. Its apparent to me that a top-level class cannot implement the interfaces nested within itself ( although I'm not sure why ).

If you want to keep the interfaces within the same file, then you have to change the modifier from public to default and declare them after the class definition.




回答3:


I see that these two interface is not default java interface, right? So, if you get the error "IPlayerity cannot be resolved to a type.", check whether you import the right packages yet. I should comment for this, but I just cant. Have fun.




回答4:


If it cannot be resolved as a type, then it cannot be "seen". You should add an import for the interface:

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

still, I have a feeling that then the compiler will throw some other weird cyclic errors..

It's better if you define the interfaces in another file.



来源:https://stackoverflow.com/questions/6711363/extending-an-inner-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!