<分区>
我正在尝试使用枚举中的所有可用值初始化一个通用类。这是我希望它的工作方式:
public class MyClass<E extends Enum<E>> {
E[] choices;
public MyClass() {
choices = E.values();
}
但是,在 Eclipse 中不接受对 E.values
的调用,表示此 E 未定义此方法。
可以接受使用此构造函数,但需要调用者提供值:
public MyClass(E[] e) {
choices = e;
}
在我找到的文档中:
Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.
有没有办法解决这个问题?