Conversion between enumerated variables and other data types in Java
When Enum is used to define an enumeration type, the actually defined type automatically inherits the java. Lang. Enum class. Each enumerated member is essentially an enumeration instance, which is public static final by default. You can use them directly by Enumeration type names. Enumeration type names can be converted into numbers or used as character matching methods to identify types. Therefore, when writing enumeration types, it can be simply understood as a string.
As follows:
Public Enum emorder {
Orone, ortwo, orclass
}
Emorder ot = emorder.Ortwo;
Incorrect practice: String STR = "I am a member of the Royal Family" + ot +"
The result is: "I am the firstOrtwoMembers entering the Royal Family"
Correct practice:String STR = "I am the first" +String. valueof (nsofttype. ordinal () + 1)+ "Members entering the Royal Family"
The result is: "I am 2nd members of the Royal Family"
------------------
Java. Lang. Enum classPublic abstract class Enum <E extends Enum <E> extends objectimplements comparable <E>. serializable is a common basic class for enumeration in all Java languages. ------------------ Constructor abstract protected Enum (string name, int ordinal) is a separate constructor. Programmers cannot call this constructor. This constructor is used for the code sent by the compiler that responds to the enumeration type declaration. Parameter: name-the name of this enumerated constant, which is the identifier used to declare the constant. Ordinal-the ordinal number of the enumerated constant (it is in the position of the enumeration Declaration, where the ordinal number of the initial constant is zero ). ------------------ Method abstract protected object clone () throws clonenotsupportedexception. Int compareto (e o) compares the order of this enumeration with the specified object. If the object is smaller than, equal to, or greater than the specified object, negative integer, zero, or positive integer are returned. Enumerated constants can only be compared with other enumerated constants of the same Enumeration type. The natural order of implementation of this method is the order in which constants are declared. Boolean equals (object other) returns true if the specified object is equal to this enumerated constant. Class <E> getdeclaringclass () returns the class object corresponding to the enumerated type of this enumerated constant. Only when e1.getdeclaringclass () = e2.getdeclaringclass (), the enumerated types of the two enumerated constants E1 and E2 are the same. (The value returned by this method is different from the value returned by the object. getclass () method. The object. getclass () method is used to enumerate constants of class subjects with specific constants .) Int hashcode () returns the hash code of the enumerated constant. String name () returns the name of this enumeration constant and declares it in its enumeration declaration. The tostring () method is preferred for most programmers, because the tostring method returns a more user-friendly name. This method is mainly designed for special cases. the correctness of this method depends on obtaining the correct name, and its name does not change int ordinal () as the version changes () returns the ordinal number of an enumerated constant (where it is in the enumeration Declaration, where the ordinal number of an initial constant is zero ). Most programmers do not use this method. It is designed for complex enumeration-based data structures, such as enumset and enummap. String tostring () returns the name of the enumerated constant, which is included in the declaration. Public static <t extends Enum <t> T valueof (class <t> enumtype, string name) returns an enumeration constant of the specified enumeration type with the specified name. The name must exactly match the identifier used to declare an enumerated constant in this type. (Additional white space characters are not allowed .) Parameter: enumtype-name of the Class Object of the enumeration type from which the constant is returned-name of the constant to be returned Note: The ordinal () method gets the index of the enumeration order, which starts from 0 by default.