Java -enumset.noneof() - 为什么使用类< e>而不是t?

发布于 2025-02-11 01:10:55 字数 631 浏览 1 评论 0原文

定义是: https://develoveling.com/reference.com/reference comleferenc./reference/java/java/java/java/util/util/enumset

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帝王念 2025-02-18 01:10:55

我知道编译时的类型(权威),所以它可以是enumset.noneof(pertional)

您不能做enumset.noneof(pertional),因为那不是合法的Java。当您要传递类型时,然后使用实例,当您在Compile time时知道所需的类时,您会使用类文字(例如,pertional.class.class.class )。


但是,为什么需要class&lt; e&gt;而不是,例如t in none(t EmplyType)

从概念上讲,当您想要一个 empty 的类型集时,需要一种类型的实例是没有意义的。但是然后我想这个问题变成了:“为什么首先需要任何论点?”。毕竟,您可以做类似的事情:

Set<String> set = new HashSet<>();

无需传递class实例。

原因是因为enumset是高度专业的set实现。它需要知道所有现有的enum常数,即使设置为空,也可以使用class进行查询。同样,实现仅适用于单型enumset不能包含来自不同enum类型的常数。 允许实现来执行此功能。

其他方法,例如(e)的enumset#,不需要class,因为它们需要enum的实例(即一个或更多常数),可以从中抓住类型。


由于它似乎需要,所以为什么不在类&lt;?&gt;

中使用Anytype

因为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;返回。

I know the type (Authority) at compile time,so could it have been EnumSet.noneOf(Authority) ?

You can't do EnumSet.noneOf(Authority) because that's not legal Java. When you want to pass around a type then you use a Class instance, and when you know the needed class at compile-time, you use a class literal (e.g., Authority.class).


But why does it require Class<E> and not, say, T as in noneOf(T elementType) ?

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:

Set<String> set = new HashSet<>();

Without passing a Class instance.

The reason is because EnumSet is a highly specialized Set implementation. It needs to know of all the existing enum constants, which can be queried using the Class, even when the set is empty. Also, the implementation only works with a single type. An EnumSet cannot contain constants from different enum types. The Class allows the implementation to enforce this.

Other methods, such as EnumSet#of(E), don't require a Class because they require instances of the enum (i.e., one or more constants) and the type can be grabbed from them.


Since it seems to require though,why not use the anytype as in Class<?>

Because a Class<?> would allow you to pass any type, not just an enum type. Though of course that could be solved by bounding the wildcard: Class<? extends Enum<?>>. Now only enum types are allowed, but the static type system has no idea which enum type you're using. This means methods such as noneOf would only be capable of returning an EnumSet<?>, which is not very useful.

By declaring a type variable E and bounding it to extend Enum<E>, the use of Class<E> forces you to pass an enum type and you get an EnumSet<E> back.

无声静候 2025-02-18 01:10:55

其他答案似乎并没有对您的问题进行慈善解释,因此我会添加一个。

但是,为什么它需要上课而不是t none(t EmplyType)?

这是因为枚举需要以后能够验证类型。例如:

var set = EnumSet.noneOf(Authority.class);
set.add(Authority.ADMIN); // ordinal 1

在第二行中,该集合需要验证被添加到集合中的枚举与其他枚举相同。如果有人后来写道,就不会有一种检查类型匹配的方法:

set.add(Color.BLUE) // ordinal 1, oops!

没有枚举类,就无法区分问题。您问一个有效的问题,为什么这应该是(轻微重写)的类型:

enumset.noneof(pertional.admin)

这是可能的,除了它对空的枚举不起作用:

enum Empty {}

最后,重要的是要注意每个枚举的值可能具有不同的类别:

enum Authority {
  ADMIN {
    @Override void checkPermissions() {...}
  },
  USER {
    @Override void checkPermissions() {...}
  };

  abstract void checkPermissions();
}
assert Authority.ADMIN.getClass() != Authority.USER.getClass();

结果,最大的 最外部类需要传递到enumset中,因此首选类对象。

The other answers don't seem to give charitable interpretations of your question, so I'll add one.

But why does it require Class and not, say, T as in noneOf(T elementType) ?

It's because the EnumSet needs to be able to verify the type later. For example:

var set = EnumSet.noneOf(Authority.class);
set.add(Authority.ADMIN); // ordinal 1

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:

set.add(Color.BLUE) // ordinal 1, oops!

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):

EnumSet.noneOf(Authority.ADMIN)

This would be possible, except it wouldn't work for empty enums:

enum Empty {}

Finally, it's important to note the values from each enum may have different classes:

enum Authority {
  ADMIN {
    @Override void checkPermissions() {...}
  },
  USER {
    @Override void checkPermissions() {...}
  };

  abstract void checkPermissions();
}
assert Authority.ADMIN.getClass() != Authority.USER.getClass();

As a result, the outermost class needs to be passed into EnumSet, so the class object is preferred.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文