证明枚举课程是枚举类,当他们通过或存储时
如果我传递到一种方法或存储在变量中,那么我知道的所有类都是枚举的集合(理想保证,但我不介意铸造),是否可以让编译器知道它们是枚举类,为了使用values()
,enumset.allof()
等的方法?即,我正在寻找无法直接参考枚举类的名称的情况。
我相信这是不可能的,但想验证。
一个示例可能看起来像这样:
enumClasses.stream()
.map(eclass -> EnumSet.allOf(eclass))
... more here...
但是我看不出一种在枚举(作为变量或参数)声明中证明它只是枚举。
示例:我尝试过的某些情况,这些情况无法使用class;? lt;?
List<? extends Class<? extends Enum<?>>> enums = List.of(MyEnum.class);
enums.forEach(eclass -> EnumSet.allOf(eclass)); // error here.
&
Class<? extends Enum<?>> enumClass = MyEnum.class;
EnumSet.allOf(enumClass); // error here.
enumClass.values(); // error here.
了Enum
static <E extends Enum<E>> EnumSet myValues(Class<E> iEnumClass) {
return EnumSet.allOf(iEnumClass);
}
扩展 >该方法(除非我直接使用类名来调用该方法,例如myValues(myenum.class)
)
If I pass to a method, or store in a variable, a collection of classes that I know are all Enums (ideally guaranteed, but I don't mind casting), is there a way to let the compiler know they are enum classes, in order to use methods like values()
, EnumSet.allOf()
, etc? I.e., I'm looking at cases where I cannot refer to the names of the Enum classes directly.
I believe this is not possible, but wanted to verify.
An example might look something like this:
enumClasses.stream()
.map(eclass -> EnumSet.allOf(eclass))
... more here...
but I don't see a way to prove in the declaration of enumClasses (as variable or parameter) that it's only enums.
Examples: Some cases I tried that did not work using Class<? extends Enum<?>>>
List<? extends Class<? extends Enum<?>>> enums = List.of(MyEnum.class);
enums.forEach(eclass -> EnumSet.allOf(eclass)); // error here.
or
Class<? extends Enum<?>> enumClass = MyEnum.class;
EnumSet.allOf(enumClass); // error here.
enumClass.values(); // error here.
I also tried creating this helper-method signature:
static <E extends Enum<E>> EnumSet myValues(Class<E> iEnumClass) {
return EnumSet.allOf(iEnumClass);
}
and the method compiles fine, but I have the same problems as above when I try to call the method (unless I call that method directly with the class-name, like myValues(MyEnum.class)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在a 链接问题
class> class> class
称为getEnumconstants()上有一种方法,
(如果不是枚举,它只是返回null,但在我的情况下,我知道它是。
isenum()
(如果需要)。正如我在标题中提出的那样,但它确实解决了我的用例,这是为了收集几个相同面积的枚举的值。
I discovered in a linked question that there's a method on
Class
calledgetEnumConstants()
(It simply returns null if it's not an enum, but in my case I know that it is.
isEnum()
is also provided if needed.)This doesn't directly solve the question as I posed it in the title, but it does solve the use-case I had, which was to collect the values of several same-interface enums.
您存在的问题是在两个
enumset.allof()
上都存在的&lt; e扩展了enum&lt; e&gt;&gt;
(以及enumset
类本身)和您的myValues()
方法。基本上,您需要一个“类的列表,每个类别的enum
本身”,并且无法在Java中表达出来。使用
class&lt;?扩展了enum&lt;?&gt;&gt;
,您正在放弃按本身继承enum
的要求,因此可以将一些无效的值放在其中。例如,某人可以将enum.class
本身(代表enum
类本身)放置在其中。或者,有人可以用枚举常数进行枚举,以实现自定义方法。这样的枚举常数将是枚举的匿名子类的一个实例。因此,在此上运行.getClass()
将为您提供class
,它是enum
的子类,但它不是它的实际类您想要的枚举。如果您想安全地保留枚举类的列表,则可能必须定义自己的自定义类
listofenumclasses
或类似的东西。add()
方法将是通用的,并在类型变量上执行关系,例如内部您仍然可以将其视为
list&lt; class&lt;?扩展Enum&lt;?&gt;&gt;
,但您不会将此变量暴露于外部。要将其中一个元素传递到enumset.allof()
内部之类的内容,您可能必须将其施放到原始类型中才能关闭仿制药,例如:enumset.allof(class(class) )eclass)
The problem you are having is with the bound
<E extends Enum<E>>
, which exists on bothEnumSet.allOf()
(as well as theEnumSet
class itself) and yourmyValues()
method. Basically, you need a "list of classes, each of which inheritsEnum
of itself", and there is no way to express that in Java.With
Class<? extends Enum<?>>
, you are giving up the requirement that the class inheritEnum
of itself, so it's possible to put some invalid values in there. For example, someone could putEnum.class
itself (the class object representing theEnum
class itself) in there. Or, someone could take an enum with an enum constant that implements custom methods; such an enum constant would be an instance of an anonymous subclass of the enum. So running.getClass()
on this would give you aClass
which is a subclass ofEnum
, but it is not the actual class of the enum that you want.If you want to hold a list of enum classes safely, you would probably have to define your own custom class
ListOfEnumClasses
or something like that. Theadd()
method would be generic and enforce the relationship on the type variable, likeInternally you might still hold it as a
List<Class<? extends Enum<?>>
, but you would not expose this variable to the outside. To pass one of the elements to something likeEnumSet.allOf()
internally, you would probably have to cast it to a raw type to turn off generics, like:EnumSet.allOf((Class) eclass)