Example:
Packagecom.dxz.enumtest; Public enumenumtest {duanxz ("The given name of Me"), DXZ ("The family name of Me"); PrivateString context; PrivateString GetContext () {return This. Context; } PrivateEnumtest (String context) { This. Context =context; } Public Static voidMain (string[] args) { for(Enumtest name:EnumTest.values ()) {SYSTEM.OUT.PRINTLN (name+ " : " +Name.getcontext ()); } System.out.println (EnumTest.DUANXZ.getDeclaringClass ()); }}
Analysis of enumeration implementations in Java:
Example:
Package com.dxz.enumtest; Public enum Color { Red,blue,black,yellow,green}
Obviously, theenum is very much like a special class, in fact the type of the enum declaration definition is one. These classes are subclasses of the Enum class in the class library (java.lang.enum<e>). They inherit many of the useful methods in this enum. After we compiled the code, we found that the compiler compiled the enum type into a single bytecode file: Color.class.
Color Bytecode Code
Final enumCom.dxz.enumtest.Color {//all enumeration values are class static constants Public Static Final enumHr.test.Color RED; Public Static Final enumHr.test.Color BLUE; Public Static Final enumHr.test.Color BLACK; Public Static Final enumHr.test.Color YELLOW; Public Static Final enumHr.test.Color GREEN; Private Static Finalsynthetic com.dxz.enumtest.color[] enum$values; //initialization process, first initialization of all enumerated value objects of the enumeration class Static { 0NewHr.test.Color [1] 3DUP4 LDC <string "RED" > [16]//presses the enumeration value string "RED" into the operand stack6 Iconst_0//pressing an integer value of 0 into the operand stack7 invokespecial Hr.test.Color (java.lang.String,int) [17]//call the private constructor of the color class to create a color object redTen putstatic Hr.test.Color.RED:hr.test.Color [21]//assigns an enumeration object to the static constant red of color. ......... Enumerating objects blue and so on102return }; //private constructors, it is not possible to dynamically create an enumeration class object (that is, it is not possible to dynamically create an enumeration value). PrivateColor (java.lang.String arg0,intarg1) { //the protected constructor that invokes the parent class enum creates an enumeration object3 invokespecial java.lang.Enum (java.lang.String,int) [38] }; Public Statichr.test.color[] VALUES (); //an abstract method for implementing an Enum class Public Staticcom.dxz.enumtest.Color valueOf (java.lang.String arg0); }
The following is a detailed description of the characteristics and usages of enum classes defined by the enum. (color example is used later)
1, the Color enumeration class is class, and is a final class that cannot be inherited. Its enumeration value (Red,blue ...) are all class static constants of the color type, we can get an instance of the color enum class in the following way:
Note: These enumeration values are public static final, which is the constant way that we often define, so the enumeration values in the enumeration class are best capitalized.
2, the enumeration class is class, of course, there are constructors, methods and data fields in the enumeration type. However, the constructors of enum classes are very different:
(1) The constructor is called only when the enumeration value is constructed.
Java code
enumcolor{RED (255,0,0), BLUE (0,0,255), BLACK (0,0,0), YELLOW (255,255,0), GREEN (0,255,0); //constructs an enumeration value, such as red (255,0,0) PrivateColor (intRvintGvintBV) { This. redvalue=RV; This. greenvalue=GV; This. bluevalue=v; } PublicString toString () {//ToString () that overrides the parent class enum return Super. toString () + "(" +redvalue+ "," +greenvalue+ "," +bluevalue+ ")"; } Private intRedvalue;//custom data fields, private for encapsulation. Private intGreenvalue; Private intBluevalue; }
(2) The constructor can only be private, and there is absolutely no public constructor allowed. This guarantees that external code cannot newly construct an instance of the enumeration class. This is perfectly reasonable, because we know that the enumeration value is a constant of public static final. However, the methods and data fields of the enumeration class can allow external access.
Java code
Public Static void Main (String args[]) { // color colors=new color (100,200,300); // wrong Color color=color.red; SYSTEM.OUT.PRINTLN (color); // the ToString () method was called
3. All enum classes inherit the Enum method, which we'll describe in detail below.
(1) ordinal () method: Returns the order of enumeration values in the enumeration class. This order depends on the order in which the enumeration values are declared.
Color.RED.ordinal (); // results returned: 0 Color.BLUE.ordinal (); // results returned: 1
(2) CompareTo () method: Enum implements the Java.lang.Comparable interface, so you can compare the order of objects like the specified object. The CompareTo in the enum returns the difference in order of the two enumeration values. Of course, the premise is that two enumeration values must belong to the same enumeration class, otherwise a classcastexception () exception will be thrown. (Visible source code)
Color.RED.compareTo (Color.Blue); // return result-1
(3) The values () method: A static method that returns an array that contains all the enumeration values.
Color[] colors=color.values (); for (Color c:colors) { System.out.print (c+ ",");} // results returned: Red,blue,black Yellow,green,
(4) ToString () method: Returns the name of the enumeration constant.
Color c=color.red; System.out.println (c); // return Result: RED
(5) ValueOf () Method: This method is relative to the ToString method and returns an enumeration constant of the specified enumeration type with the specified name.
Color.valueof ("BLUE"); // return Result: Color.Blue
(6) Equals () method: Compares references to two enum class objects.
// JDK Source code: Public Final Boolean equals ( Object) {returnthis = = other;
4. Enumeration classes can be used in switch statements.
Color color=color.red; Switch (color) { case RED:System.out.println ("It ' s RED") ; break; Case BLUE:System.out.println ("It s Blue"); Break ; Case BLACK:System.out.println ("It s Blue"); Break ; }
The use and analysis of enum in Java