//Use an enumeration class instead of a static constant to
Enum classes are no longer inherited, which is final
Public enumTestenum {//enumeration values appear in the first place, static constantsRED (255,0,0), BLUE (0,0,255), GREEN (0,255,0); //provides a private constructor PrivateTestenum (intRvintGvintBV) { This. Redvalue =RV; This. Greenvalue =GV; This. Bluevalue =BV; } //Custom data fields Private intRedvalue; Private intGreenvalue; Private intBluevalue; Public intGetredvalue () {returnRedvalue; } Public intGetgreenvalue () {returnGreenvalue; } Public intGetbluevalue () {returnBluevalue; }}
All enum classes inherit the method of the enum
(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+ ",");
}//return Result: 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.
Enum in Java