Enum class member with interface cannot find methods internally

匿名 (未验证) 提交于 2019-12-03 08:36:05

问题:

I'm having a strange issue which I'm not sure if it's a compiler problem or my understanding of enums with interfaces. I'm using IntelliJ IDEA 12, building an Android project, and I have a class like this:

public class ClassWithEnum {     private MyEnum myEnum;      //Trying to access it internally here throws the error     public boolean isActionable() {         return myEnum.isActionable();     }      public enum MyEnum implements Action {         ACTIONABLE() {             @Override             public boolean isActionable() { return true; }         },         NOT_ACTIONABLE() {             @Override             public boolean isActionable() { return false; }         }     }      public interface Action {         public boolean isActionable();     } }

Now, this was working initially, but now the compiler is complaining (and I've tried this in a brand new project as well with the same results) with the error:

java: /Users/kcoppock/Documents/Projects/EnumInterfaceTest/src/com/example/EnumInterfaceTest/ClassWithEnum.java:11: cannot find symbol symbol  : method isActionable() location: class com.example.EnumInterfaceTest.ClassWithEnum.MyEnum

I've done this before (enumerations with behaviors defined by an interface) with no issues. Any thoughts?

回答1:

You need to implement isActionable() method in MyEnum itself. Because the method isActionable() defined inside the ACIONABLE and NOT_ACTIONABLE are local to them . So you need the global method for the MyEnum enum .

use this code instead:

public enum MyEnum implements Action {         ACTIONABLE() {             @Override             public boolean isActionable() { return true; }         },         NOT_ACTIONABLE() {             @Override             public boolean isActionable() { return false; }         };         @Override         public boolean isActionable() { return false;}     }


回答2:

You could try this alternative:

   public enum MyEnum implements Action {         ACTIONABLE(true),          NOT_ACTIONABLE(false);          private final boolean actionable;          MyEnum(boolean actionable) {            this.actionable = actionable;         }          @Override         public boolean isActionable() {             return this.actionable;          }     }


回答3:

That looks like a javac bug. javac compiles it fine in JDK7. It doesn't in JDK6 (javac 1.6.0_24), both from IntelliJ and from the command line.



回答4:

It seems that javac for 1.6 requires a global override for the interface method, whereas later versions do not, nor does the Eclipse compiler.

Seems it was just a bug with IntelliJ. I changed the compiler setting from javac to Eclipse, then back to javac and it compiles and runs as expected.

False alarm. :)



回答5:

Some people already answered this perfectly, I just add one more little tip: you can also keep the method abstract in the interface:

public enum MyEnum implements Action {     ACTIONABLE() {         @Override         public boolean isActionable() { return true; }     },     NOT_ACTIONABLE() {         @Override         public boolean isActionable() { return false; }     };     @Override     abstract public boolean isActionable(); }


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