[Java]
Public class TestEnum {
/* The most common enumeration */
Public enum ColorSelect {
Red, green, yellow, blue;
}
/* Enumeration can also add methods and attributes like a general class. You can add static and non-static attributes or methods for it, it's all like what you do in a general class. */
Public enum Season {
// The enumeration list must be written at the beginning; otherwise, an error occurs during compilation.
Winter, spring, summer, fall;
Private final static String location = "Phoenix ";
Public static Season getBest (){
If (location. equals ("Phoenix "))
Return winter;
Else
Return summer;
}
}
/* You can also create a constructor */
Public enum Temp {
/* Assign values through parentheses and must have a constructor with parameters and an attribute and method. Otherwise, the compilation fails.
* The values must be both assigned values or not assigned values. Some values cannot be assigned.
* If no value is assigned, the constructor cannot be written, and an error occurs in value assignment compilation */
AbsoluteZero (-459), freezing (32), boiling (212), paperBurns (451 );
Private final int value;
Public int getValue (){
Return value;
}
// The constructor can only be private by default, so that the constructor can only be used internally
Temp (int value ){
This. value = value;
}
}
Public static void main (String [] args ){
/*
* An Enumeration type is a type used to define variables. to restrict the value assignment of variables, you can use "enumeration name. value" to obtain values in the enumerated values.
*/
ColorSelect m = ColorSelect. blue;
Switch (m ){
/* Note: ToString () is overwritten in enumeration, indicating that the value of the enumerated variable does not contain a prefix.
* Therefore, it is blue rather than ColorSelect. blue.
*/
Case red:
System. out. println ("color is red ");
Break;
Case green:
System. out. println ("color is green ");
Break;
Case yellow:
System. out. println ("color is yellow ");
Break;
Case blue:
System. out. println ("color is blue ");
Break;
}
System. out. println ("traversing values in ColorSelect ");
/* Obtain the array of enumerated values through values */
For (ColorSelect c: ColorSelect. values ()){
System. out. println (c );
}
System. out. println ("enumeration ColorSelect values include:" + ColorSelect. values (). length + ");
/* Ordinal () returns the index position of the enumerated value in the enumeration, starting from 0 */
System. out. println (ColorSelect. red. ordinal (); // 0
System. out. println (ColorSelect. green. ordinal (); // 1
System. out. println (ColorSelect. yellow. ordinal (); // 2
System. out. println (ColorSelect. blue. ordinal (); // 3
/* Java. lang. Comparable interface is implemented by enumeration by default */
System. out. println (ColorSelect. red. compareTo (ColorSelect. green ));
System. out. println (Season. getBest ());
For (Temp t: Temp. values ()){
/* Get the enumerated value through getValue */
System. out. println (t + "value is" + t. getValue ());
}
}
}
Public class TestEnum {
/* The most common enumeration */
Public enum ColorSelect {
Red, green, yellow, blue;
}
/* Enumeration can also add methods and attributes like a general class. You can add static and non-static attributes or methods for it, it's all like what you do in a general class. */
Public enum Season {
// The enumeration list must be written at the beginning; otherwise, an error occurs during compilation.
Winter, spring, summer, fall;
Private final static String location = "Phoenix ";
Public static Season getBest (){
If (location. equals ("Phoenix "))
Return winter;
Else
Return summer;
}
}
/* You can also create a constructor */
Public enum Temp {
/* Assign values through parentheses and must have a constructor with parameters and an attribute and method. Otherwise, the compilation fails.
* The values must be both assigned values or not assigned values. Some values cannot be assigned.
* If no value is assigned, the constructor cannot be written, and an error occurs in value assignment compilation */
AbsoluteZero (-459), freezing (32), boiling (212), paperBurns (451 );
Private final int value;
Public int getValue (){
Return value;
}
// The constructor can only be private by default, so that the constructor can only be used internally
Temp (int value ){
This. value = value;
}
}
Public static void main (String [] args ){
/*
* An Enumeration type is a type used to define variables. to restrict the value assignment of variables, you can use "enumeration name. value" to obtain values in the enumerated values.
*/
ColorSelect m = ColorSelect. blue;
Switch (m ){
/* Note: ToString () is overwritten in enumeration, indicating that the value of the enumerated variable does not contain a prefix.
* Therefore, it is blue rather than ColorSelect. blue.
*/
Case red:
System. out. println ("color is red ");
Break;
Case green:
System. out. println ("color is green ");
Break;
Case yellow:
System. out. println ("color is yellow ");
Break;
Case blue:
System. out. println ("color is blue ");
Break;
}
System. out. println ("traversing values in ColorSelect ");
/* Obtain the array of enumerated values through values */
For (ColorSelect c: ColorSelect. values ()){
System. out. println (c );
}
System. out. println ("enumeration ColorSelect values include:" + ColorSelect. values (). length + ");
/* Ordinal () returns the index position of the enumerated value in the enumeration, starting from 0 */
System. out. println (ColorSelect. red. ordinal (); // 0
System. out. println (ColorSelect. green. ordinal (); // 1
System. out. println (ColorSelect. yellow. ordinal (); // 2
System. out. println (ColorSelect. blue. ordinal (); // 3
/* Java. lang. Comparable interface is implemented by enumeration by default */
System. out. println (ColorSelect. red. compareTo (ColorSelect. green ));
System. out. println (Season. getBest ());
For (Temp t: Temp. values ()){
/* Get the enumerated value through getValue */
System. out. println (t + "value is" + t. getValue ());
}
}
}