以下 java 代码无法在 eclipse 中编译。我在这里做错了什么?如果该方法只返回一个 int
而不是 enum
,那么一切都可以正常工作,所以它基本上设置好了。问题在于引入枚举返回类型。
public class myclass {
public enum mytype {
mytype2,
mytype1,
};
public static mytype retmytype() {
return mytype2;
}
}
//in another class
myclass.mytype t = myclass.retmytype(); //ERROR - myclass.mytype cannot be solved
最佳答案
尝试将 return mytype2;
替换为 return mytype.mytype2;
顺便说一句,您应该遵循 Java 命名约定;)
我认为您忘记了一个 main 方法(或程序流程中任何其他调用的方法)。 试试这个:
public class myclass {
public enum mytype {
mytype2,
mytype1,
};
public static mytype retmytype() {
return mytype.mytype2;
}
public static void main(String[] args){
myclass.mytype t = myclass.retmytype();
}
}
关于java - 为什么这个枚举类型没有正确解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15729716/