1. The list of enumerated elements must be written at the top of the enumeration class, separated by commas, and if there is no other content after the end of the list of elements, you may not write a semicolon or you must write.
2. All constructors in the enumeration class are private, and external programs cannot create instances of enumerated classes. An enumeration class can explicitly indicate which builder to invoke, such as member and member (), the two elements list declarations are equivalent, all calling the default builder, and the members ("ordinary") are explicitly indicating that the program calls the second builder.
3. Enumeration classes can have abstract methods, but they must be implemented in element list declarations. In addition, you can declare some ordinary member variables and methods in the enumeration class, as shown in the previous example.
Common methods in enumeration classes
1. ToString () method, display enumerated class elements, in the example of this method is covered, if not covered it general print variable name, the example above for "member", "ADMIN", "superadmin";
2. valueof (String arg0) method to parse an instance of the enumeration class by passing in the arg0 string, in which the passed-in string must be one of the names of the element lists, or it will throw Java.lang.IllegalArgumentException exceptions, this method is important in Web applications. The method is a static method and can be used without creating an instance, such as by Enumaccesscontroll.valueof ("member") to return the member instance.
3. Values () method, returns an array of the enumerated class in which the element of the array is the element in the list of elements in the enumeration class.
4. The ordinal () method returns the position of the list of elements in the enumeration element instance, starting at 0. The result of such as EnumAccessControll.SUPERADMIN.ordinal () is 2.
5. CompareTo () method, compare two elements.
The 6. Name () method, by default, returns the variable name of the element's real column, which is final and cannot be overwritten.
|
public enum color{
Red,blue,black,yellow,green
} |
|
public enum color{
Red ("Red", 0), Blue ("Blue", 1), Black ("dark", 2)
private String name;
private int value;
Private Color (String name,int value) {
This.name=name;
This.value=value;
}
public void SetName (String name) {
This.name=name;
}
Public String GetName () {
return this.name;
}
public void SetValue (int value) {
This.value=value;
}
public int GetValue () {
return this.value;
}
Public String toString () {
return name;
}
}
|
1. The Color enumeration class is a special class whose enumeration value (Red,blue ...) is a color class object (class instance):
Color c=color.red;
And these enumerated values are public static final, the constant way we often define, so the enumeration values in the enumeration class are best capitalized.
2, that is, the enumeration class is class, and of course there are constructors, methods, and data fields in the enumeration type. However, the constructors of an enumeration class differ greatly:
(1) The constructor is invoked only when the enumeration value is constructed.
(2) The constructor is private only, and the public constructor is never allowed. This guarantees that the external code cannot construct an instance of the enumeration class in a new way. 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 an enumeration class can allow external access.
3. All enumerated classes inherit the methods of the enum, and we describe them in detail below.
(1) ordinal () method: Returns the order of enumerated values in the enumeration class. This order depends on the order in which the enumeration values are declared.
Color.RED.ordinal (); Return Result: 0
Color.BLUE.ordinal (); return Result: 1
(2) CompareTo () method: An enum implements the Java.lang.Comparable interface, so it can be compared to the order of the specified object. The CompareTo in the enum returns the difference in the order of the two enumerated values. Of course, the premise is that two enumerated values must belong to the same enumeration class, otherwise the ClassCastException () exception is thrown. (Concrete visible source code)
Color.RED.compareTo (Color.Blue); return result-1
(3) VALUES () method: A static method that returns an array containing all the enumerated values.
Color[] Colors=color.values ();
for (Color c:colors) {
System.out.print (c+ ",");
}//return Result: Red,blue,black Yellow,green,
(4) ToString () method: Returns the name of an enumerated constant.
Color c=color.red;
System.out.println (c);//return Result: RED
(5) valueof () Method: This method corresponds to the ToString method and returns an enumerated constant with the specified enumeration type with the specified name.
Color.valueof ("BLUE"); return Result: Color.Blue
(6) Equals () method: Compares the references of two enumerated class objects.
Java code
JDK Source code:
Public final Boolean equals (Object other) {
return this==other;
}
JDK Source code:
Public final Boolean equals (Object other) {
return this==other;
}
4. Enumeration classes can be used in switch statements.
Java code
Color color=color.red;
switch (color) {
Case RED:System.out.println ("It s RED");
Case BLUE:System.out.println ("It ' s BLUE");
Case BLACK:System.out.println ("It ' s Blue");