简体   繁体   English

Java中的哪个类实现了抽象类EnumSet?

[英]Which class in Java implements the abstract class EnumSet?

I see that EnumSet.of() returns an instance of an object of the type EnumSet . 我看到EnumSet.of()返回EnumSet类型的对象的实例。 But I am not able to figure out which class actually implements this abstract class? 但我无法弄清楚哪个类实际实现了这个抽象类? How can you get an instance of the abstract type EnumSet, when you have not subclassed it? 当你没有子类化它时,如何获得抽象类型EnumSet的实例?

Here is are 2 classes in java which extends EnumSet 以下是java中的两个扩展EnumSet

1. RegularEnumSet
2. JumboEnumSet

You can create the instance using EnumSet's static methods like EnumSet#noneOf , EnumSet#allOf etc. Which actually returns the instance of RegularEnumSet or JumboEnumSet depending on the condition. 您可以使用EnumSet的静态方法创建实例,例如EnumSet#noneOfEnumSet#allOf等。它实际上JumboEnumSet根据条件返回RegularEnumSetJumboEnumSet的实例。 EnumSet#of internally calls EnumSet#noneOf .Please refer the below code from Java to see how EnumSet#noneOf works EnumSet#of内部调用EnumSet#noneOf 。请参考Java的以下代码,了解EnumSet#noneOf工作原理

public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) {
    Enum<?>[] universe = getUniverse(elementType);
    if (universe == null)
        throw new ClassCastException(elementType + " not an enum");

    if (universe.length <= 64)
        return new RegularEnumSet<>(elementType, universe);
    else
        return new JumboEnumSet<>(elementType, universe);
}

You do not directly create instances of EnumSet . 您不直接创建EnumSet实例。 This is done by the static factory methods, such as: 这是通过静态工厂方法完成的,例如:

enum Demo { YES, NO, FILENOTFOUND }

EnumSet<Demo> all = EnumSet.allOf(Demo.class);
EnumSet<Demo> notNo = EnumSet.of(Demo.YES, Demo.FILENOTFOUND);
EnumSet<Demo> none = EnumSet.noneOf(Demo.class);

The resulting sets are mutable, ie, you can do: 结果集是可变的,即你可以这样做:

EnumSet<Demo> set = EnumSet.noneOf(Demo.class);  // start with an empty set
...
set.add(Demo.YES);
set.remove(Demo.NO);
...
set.clear();
...

A specialized Set implementation for use with enum types. 用于枚举类型的专用Set实现。 All of the elements in an enum set must come from a single enum type that is specified, explicitly or implicitly, when the set is created. 枚举集中的所有元素必须来自单个枚举类型,该类型在创建集时显式或隐式指定。

You can't create a instance if EnumSet ! 如果EnumSet您无法创建实例! However also you don't need to extend it, for it's purpose there is a factory method to create a Set from specified elements: 但是,您也不需要扩展它,因为它的目的是有一个工厂方法从指定的元素创建一个Set:

public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest)

It creates an object for you. 它为您创建了一个对象。 Often EnumSets are used for combining effects, whether it's text styles eg BOLD and UNDERLINE or with colors... 通常EnumSets用于组合效果,无论是文本样式,例如BOLD和UNDERLINE,还是颜色......

enum Color { GREEN, RED, BLUE };
EnumSet <Color> yellow = EnumSet.of(Color.RED, Color.GREEN);

You found additional information here ... 您在此处找到了其他信息......

To find this information, you could look at all the code, or print out the getClass() of the object returned as a sample but a simpler way is to use your IDE. 要查找此信息,您可以查看所有代码,或打印出作为示例返回的对象的getClass() ,但更简单的方法是使用IDE。

Navigate to EnumSet and click the icon showing the implementations 导航到EnumSet并单击显示实现的图标

EnumSet的实现

This shows you a list of all the available sub-classes of EnumSet available. 这将显示可用EnumSet的所有可用子类的列表。

The other answers already answered what are the concrete subclasses of EnumSet that actually implement the abstract class EnumSet . 其他答案已经回答了实际实现抽象类EnumSetEnumSet的具体子类。 I will attempt to clarify how we can get an instance of the abstract type EnumSet , when we have not subclassed it. 我将尝试澄清我们如何在没有子类化的情况下获取抽象类型EnumSet的实例。

Observe that EnumSet does not have a constructor, so it is not intended to be used like a normal class: 观察到EnumSet没有构造函数,因此它不能像普通类一样使用:

EnumSet set = new EnumSet(...); // not like this

Rather, to create an EnumSet , we use one of its static initializers (such as of ). 相反,创建一个EnumSet ,我们使用它的静态初始化的一个(如of )。 These initializers will internally create an object of type either JumboEnumSet (if the number of enum constants is large) or RegularEnumSet (otherwise) and return the reference of that object. 这些初始值设定项将在内部创建一个类型为JumboEnumSet (如果枚举常量的数量很大)或RegularEnumSet (否则)的对象,并返回该对象的引用。

Since JumboEnumSet and RegularEnumSet are both subclasses of EnumSet , they can be assigned to a variable of type EnumSet (this is known as widening reference conversion ). 由于JumboEnumSetRegularEnumSet都是EnumSet子类,因此可以将它们分配给EnumSet类型的变量(这称为扩展引用转换 )。

The beauty of this is that when we use the variable of type EnumSet , we do not have to know which concrete implementation we are using. 这样做的EnumSet是,当我们使用EnumSet类型的变量时,我们不必知道我们正在使用哪个具体实现。 In fact, both JumboEnumSet and RegularEnumSet are both private class in the package java.util so we can't even create it directly. 实际上, JumboEnumSetRegularEnumSet都是包java.util私有类,所以我们甚至无法直接创建它。

You can explore more how EnumSet is implemented internally through its source (see, for instance here for the Java 9's version ). 您可以通过其源代码了解EnumSet内部实现的更多信息(例如,参见Java 9的版本 )。

Another example of commonly use type in Java that is not actually a concrete class is Stream . Java中常用类型的另一个例子是Stream Stream is actually an interface. Stream实际上是一个接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM