Use of enumeration in java

 The full name of enum is enumeration, which is a new feature introduced in JDK 1.5 and stored in the java.lang package.

 To create an enumeration type, use the enum keyword, which implies that the created type is a subclass of the java.lang.Enum class (java.lang.Enum is an abstract class). An enumeration type conforms to the generic schema  Class Enum<E extends Enum<E>>and  E represents the name of the enumeration type. Each value of the enumeration type is mapped into the  protected Enum(String name, int ordinal) constructor, where the name of each value is converted to a string, and the ordinal setting indicates the order in which the setting was created.

Let's define an enumeration type:

package test;
public enum Week {
     MOn, Tue, WED, THU, TRI, SAT, SUN;	
}
 The code to traverse the enumeration type is as follows:
public class Test3  {
	public static void main(String[] args) {
		for(Week w:Week.values()){
			System.out.println(w.);
		}
	}
}

 Introduction to common methods of enum objects

int compareTo(E o) Compares the order of this enumeration with the specified object.

Class<E> getDeclaringClass() Returns the Class object corresponding to the enumeration type of this enumeration constant.

String name() returns the name of this enum constant, declared in its enum declaration.

int ordinal() returns the ordinal of the enumeration constant (its position in the enumeration declaration where the initial constant ordinal is zero).

String toString() returns the name of the enum constant, which is included in the declaration.

static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) 

                                                   Returns an enum constant of the specified enum type with the specified name.

Principle analysis

        Although the grammatical structure of enum is different from that of class, a class file is generated after being compiled by the compiler. After decompiling the class file, you can see that a class is actually generated, which inherits java.lang.Enum<E>. The contents of EnumTest after decompilation ( javap com.hmw.test.EnumTest  command) are as follows:

public class test.Week extends java.lang.Enum{
    public static final test.Week MON;
    public static final test.Week TUE;
    public static final test.Week WED;
    public static final test.Week THU;
    public static final test.Week FRI;
    public static final test.Week SAT;
    public static final test.Week SUN;
    static {};
    public int getValue();
    public boolean isRest();
    public static test.Week[] values();
    public static test.Week valueOf(java.lang.String);
    test.Week(java.lang.String, int, int, test.Week);
}

 So, in fact, enum is a class, but the java compiler parses and compiles the grammar for us.

Summarize

    Enum can be regarded as an ordinary class, they can define some attributes and methods, the difference is: enum cannot use extends keyword to inherit other classes, because enum has inherited java.lang.Enum (java is single inheritance ).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326093626&siteId=291194637