Java -enumset.noneof() - 为什么使用类< e>而不是t?
noneOf(Class<E> elementType)
Creates an empty enum set with the specified element type.
但是,为什么需要class&lt; e&gt;
而不是,而不是none(t EmplyType)
中的t? 是因为它需要在运行时输入类型吗? 但是,当我编写代码时,例如enumset.noneof(pertioner.class);
我知道编译时的类型(授权),所以它可以是enumset.noneof(pertional)?
既然它似乎需要,为什么不在类&lt;?
The definition is:
https://developer.android.com/reference/java/util/EnumSet
noneOf(Class<E> elementType)
Creates an empty enum set with the specified element type.
But why does it require Class<E>
and not, say, T as in noneOf(T elementType)
?
Is it because it requires the type to be entered at runtime?
But when I write code for example EnumSet.noneOf(Authority.class);
I know the type (Authority) at compile time,so could it have been EnumSet.noneOf(Authority)
?
Since it seems to require though,why not use the anytype as in Class<?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能做
enumset.noneof(pertional)
,因为那不是合法的Java。当您要传递类型时,然后使用类
实例,当您在Compile time时知道所需的类时,您会使用类文字(例如,pertional.class.class.class )。
从概念上讲,当您想要一个 empty 的类型集时,需要一种类型的实例是没有意义的。但是然后我想这个问题变成了:“为什么首先需要任何论点?”。毕竟,您可以做类似的事情:
无需传递
class
实例。原因是因为
enumset
是高度专业的set
实现。它需要知道所有现有的enum
常数,即使设置为空,也可以使用class
进行查询。同样,实现仅适用于单型。enumset
不能包含来自不同enum
类型的常数。类
允许实现来执行此功能。其他方法,例如(e)的
enumset#,不需要
class
,因为它们需要enum
的实例(即一个或更多常数),可以从中抓住类型。因为
class&lt;?&gt;
允许您通过任何类型,而不仅仅是enum
类型。虽然当然可以通过界限通配符来解决:class&lt;扩展枚举&lt;?&gt;&gt;
。现在,只允许enum
类型,但是静态类型系统不知道您正在使用的enum
类型。这意味着诸如nonof
之类的方法只能返回enumset&lt;?&gt;
,这不是很有用。通过声明类型变量
e
并将其界限到扩展enum&lt; e&gt;
,使用class&lt; e&gt;
迫使您传递<<<代码>枚举 type 和您获得enumset&lt; e&gt;
返回。You can't do
EnumSet.noneOf(Authority)
because that's not legal Java. When you want to pass around a type then you use aClass
instance, and when you know the needed class at compile-time, you use a class literal (e.g.,Authority.class
).Conceptually, it makes no sense to require an instance of a type when you want an empty set of said type. But then I suppose the question becomes, "Why is any argument needed in the first place?". After all, you can do something like:
Without passing a
Class
instance.The reason is because
EnumSet
is a highly specializedSet
implementation. It needs to know of all the existingenum
constants, which can be queried using theClass
, even when the set is empty. Also, the implementation only works with a single type. AnEnumSet
cannot contain constants from differentenum
types. TheClass
allows the implementation to enforce this.Other methods, such as
EnumSet#of(E)
, don't require aClass
because they require instances of theenum
(i.e., one or more constants) and the type can be grabbed from them.Because a
Class<?>
would allow you to pass any type, not just anenum
type. Though of course that could be solved by bounding the wildcard:Class<? extends Enum<?>>
. Now onlyenum
types are allowed, but the static type system has no idea whichenum
type you're using. This means methods such asnoneOf
would only be capable of returning anEnumSet<?>
, which is not very useful.By declaring a type variable
E
and bounding it to extendEnum<E>
, the use ofClass<E>
forces you to pass anenum
type and you get anEnumSet<E>
back.其他答案似乎并没有对您的问题进行慈善解释,因此我会添加一个。
这是因为枚举需要以后能够验证类型。例如:
在第二行中,该集合需要验证被添加到集合中的枚举与其他枚举相同。如果有人后来写道,就不会有一种检查类型匹配的方法:
没有枚举类,就无法区分问题。您问一个有效的问题,为什么这应该是(轻微重写)的类型:
这是可能的,除了它对空的枚举不起作用:
最后,重要的是要注意每个枚举的值可能具有不同的类别:
结果,最大的 最外部类需要传递到
enumset
中,因此首选类对象。The other answers don't seem to give charitable interpretations of your question, so I'll add one.
It's because the EnumSet needs to be able to verify the type later. For example:
On the second line, the set needs to verify that enum being added to the set is the same type as the others. There wouldn't be a way to check the types match if someone later wrote:
It wouldn't be possible to distinguish the problem without the enum class. You ask a valid question of why this should be the type with (lightly rewritten):
This would be possible, except it wouldn't work for empty enums:
Finally, it's important to note the values from each enum may have different classes:
As a result, the outermost class needs to be passed into
EnumSet
, so the class object is preferred.