Java enumeration usage

Source: Internet
Author: User

[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 ());
}

}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.