Enum (enumeration) Example

Source: Internet
Author: User

Package main;

Public class EnumTest {

/**
* Normal Enumeration
*/
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.
*/
Public enum SeasonEnum {
// Note: enumeration is written at the beginning; otherwise, an error occurs during compilation.
Spring, summer, autumn, winter;

Private static SeasonEnum position = SeasonEnum. spring;

Public static SeasonEnum getSeason (){
Return position;
}

Public static void setSeason (SeasonEnum season ){
Position = season;
}
}

/**
* Implement enumeration with Constructors
*/
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;
}
}

/**
* Implement enumeration with abstract methods
*/
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 ("============= ");
SeasonEnum. setSeason (SeasonEnum. summer );
System. err. println ("Season" + SeasonEnum. getSeason ());

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

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

}

Related Article

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.