Java enumeration Usage Details

Source: Internet
Author: User

Java enumeration Usage Details

Detailed usage of Ruthless java Enumeration

 

In actual programming, there are often such "datasets", their values are stable in the program, and the elements in the "dataset" are limited.

For example, seven data elements from Monday to Sunday constitute a "dataset" for a week, and four data elements for spring, summer, and autumn constitute a "dataset" for the Four Seasons ".

How can we better use these "datasets" in java? The following code details the usage of enumeration.

Package com. ljq. test;

/**
* Enumeration usage
*
* @ Author jiqinlin
*
*/
Public class TestEnum {
/**
* Normal Enumeration
*
* @ Author jiqinlin
*
*/
Public enum ColorEnum {
Red, green, yellow, blue;
}

/**
* You can add attributes and methods like ordinary classes for enumeration. You can add static and non-static attributes or methods for it.
*
* @ Author jiqinlin
*
*/
Public enum SeasonEnum {
// Note: enumeration is written at the beginning; otherwise, an error occurs during compilation.
Spring, summer, autumn, winter;

Private final static String position = "test ";

Public static SeasonEnum getSeason (){
If ("test". equals (position ))
Return spring;
Else
Return winter;
}
}

/**
* Gender
*
* Implement enumeration with Constructors
*
* @ Author jiqinlin
*
*/
Public enum Gender {
// Assign values through parentheses and must contain a parameter constructor and an attribute and method. Otherwise, the compilation fails.
// Values must be assigned either or not. Some values cannot be assigned but some cannot be assigned. If values are not assigned, the constructor cannot be written, and value assignment compilation also fails.
MAN ("MAN"), WOMEN ("WOMEN ");

Private final String value;

// The constructor can only be private by default, so that the constructor can only be used internally
Gender (String value ){
This. value = value;
}

Public String getValue (){
Return value;
}
}

/**
* Order status
*
* Implement enumeration with abstract methods
*
* @ Author jiqinlin
*
*/
Public enum OrderState {
/** Canceled */
CANCEL {public String getName () {return "canceled ";}},
/** Awaiting review */
WAITCONFIRM {public String getName () {return "awaiting review ";}},
/** Pending payment */
WAITPAYMENT {public String getName () {return "Pending payment ";}},
/** Goods being delivered */
ADMEASUREPRODUCT {public String getName () {return "goods in process ";}},
/** Waiting for delivery */
WAITDELIVER {public String getName () {return "waiting for shipment ";}},
/** Delivered */
DELIVERED {public String getName () {return "DELIVERED ";}},
/** Received */
Inclued {public String getName () {return "RECEIVED ";}};

Public abstract String getName ();
}

Public static void main (String [] args ){
// Enumeration is a type used to define variables to restrict the assignment of variables. When assigning values, the enumerated values are obtained through "enumeration name. value ".
ColorEnum colorEnum = ColorEnum. blue;
Switch (colorEnum ){
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;
}

// Traverse Enumeration
System. out. println ("traversing values in the ColorEnum enumeration ");
For (ColorEnum color: ColorEnum. values ()){
System. out. println (color );
}

// Obtain the number of enumerations
System. out. println ("the values in the ColorEnum enumeration are" + ColorEnum. values (). length + ");

// Obtain the enumerated index position. The default value starts from 0.
System. out. println (ColorEnum. red. ordinal (); // 0
System. out. println (ColorEnum. green. ordinal (); // 1
System. out. println (ColorEnum. yellow. ordinal (); // 2
System. out. println (ColorEnum. blue. ordinal (); // 3

// Java. lang. Comparable interface is implemented by enumeration by default.
System. out. println (ColorEnum. red. compareTo (ColorEnum. green); //-1

//--------------------------
System. out. println ("============= ");
System. err. println ("Season" + SeasonEnum. getSeason ());


//--------------
System. out. println ("============= ");
For (Gender gender: Gender. values ()){
System. out. println (gender. value );
}

//--------------
System. out. println ("============= ");
For (OrderState order: OrderState. values ()){
System. out. println (order. getName ());
}
}

}

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.