我试图用一个枚举的所有可用值来初始化一个泛型类. HEre是我想要的工作方式:
public class MyClass> {
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.
有没有办法解决这个问题?