Java – confused about extending the generics of existing enum types

Question 1: I'm a little confused about these codes:

public class MyClass1 <E extends Enum<?>> {
    // ...
}
public class MyClass2 <E extends Enum<E>> {
    // ... 
}

What are the differences between myclass1 and myclass2? What do these three different e mean?

Question 2: start with class enum < e extends enum < E > >

But not a common base class of all enum types enum < E >?

Question 3: now I have this course:

public class MyClass<E extends Enum<E>> {

    // for example,EnumSet<Category> 
    public Set<E> category; 

    // how to initial this member
    private Class<E> enumType; 

    // initial this.category with given strings
    public void getEnumSetFromStringList(List<String> list) {
        this.category = EnumSet.noneOf(enumType);
        for (String str : list) {
            this.category.add(Enum.valueOf(this.enumType,str));
        }
    }
}

// this is the Category of Enum type
public enum Category {

    LOCATION("LOCATION"),HUMAN("HUMAN"),// ...
    DESCRIPTION("DESCRIPTION");

    private final String categoryName;

    Category(String categoryName) {
        this.categoryName= categoryName;
    }
}

>How do I initialize the data field enumtype in MyClass? > If MyClass does not contain the data field enumtype, how can the function getenumsetfromstringlist (list < string > list) obtain the type of generic type < e extensions enum ? > I can get enum type in this way: e.class? If not, why was it erased from the generic type during compilation?

Solution

Enum < e extension enum < E > > is difficult to understand

Let's consider two enumerations

Enumerate colors {red, green, blue}

Enumerate shapes {square, circle, triangle}

Think of the CompareTo () method in enum This is used to compare different constants in the same enumeration according to the declared order If enum is not a generic class, the signature of this method must be int CompareTo (enum E) But it doesn't make any sense, because then you can compare color with shape We want the CompareTo signature in color to be int CompareTo (color) The only way is if enum is a generic class with type parameter E and the type parameter of color is color! As long as the type parameter of color is color and the type parameter of shape is shape, we can only compare color with color and shape with shape

Therefore, in the definition of enum, we want some expressions. For each subclass, the type parameter must not only be enumeration, but also the subclass itself Therefore, e should not only extend enum, but e should extend enum < E >!

This is where enum < e extends enum < E > > come from.

(you shouldn't write enum < e extends enum )

One way to assign a value to enumtype is to pass class < E > To constructor like this

class MyClass2<E extends Enum<E>> {
    private final Class<E> enumType;

    MyClass2(Class<E> enumType) {
        this.enumType = enumType;
    }
}

If you don't want to do this, another way to get the class < E > runtime object is to use the Enum instance method getdeclaraingclass() This requires that you have an e instance at hand to call the method One way to get an array of all enumerated constants at run time is to write e.getdeclaraingclass() getEnumConstants().

You cannot write e.class because, as you said, generics are implemented using the type erasure process At runtime, ArrayList < string > is just an ArrayList, so the type parameter cannot be accessed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>