In practical programming, such "datasets" often exist, their values are stable in the program, and the elements in the dataset are finite.
For example, from Monday to Sunday, seven data elements constitute a week of "data sets", Four seasons of four data elements constitute the "dataset".
How do you better use these "datasets" in Java? So the enumeration comes in handy, and the following code details the use of enumerations.
Package com.ljq.test;
/** * Enumeration Usage * * @author jiqinlin * */public class Testenum {/** * General enumeration * * @author Jiqinlin *
* * Public enum Colorenum {red, green, yellow, blue;
/** * Enumerations can add properties and methods as normal classes can add both static and Non-static properties or methods * * @author Jiqinlin */public enum Seasonenum {
Note: The enumeration is written at the front, otherwise the compilation error spring, summer, autumn, winter;
Private final static String position = "Test";
public static Seasonenum Getseason () {if (' Test '. Equals (position)) return spring;
else return winter; }/** * Gender * implements an enumeration with constructors * * @author jiqinlin * */public enum gender{//is assigned by parentheses, and must
Must be accompanied by a parameter constructor and a property and method, otherwise, the compilation error//assignment must be assigned or not assigned value, not part of the assignment is not assigned value; If you do not assign a value can not write the constructor, assignment compilation Error Man ("Mans"), WOMEN ("WOMEN");
Private final String value;
The constructor defaults to private only, guaranteeing that the constructor can only use Gender (String value) {this.value = value internally; Public String GetValue () {return ValUe
/** * Order Status * Implement an enumeration with an abstract method * * @author Jiqinlin * */public enum Orderstate {/** cancelled */ Cancel {public string GetName () {return ' canceled ';}},/** to be audited/waitconfirm {public string getName () {return "pending review"; },/** waiting for payment/waitpayment {public String getName () {return "Waiting for Payment";}},/** is distributing goods * * Admeasureproduct {Publi C String GetName () {return "is being shipped";}},/** waiting for shipment/waitdeliver {public String getName () {return "Waiting for Delivery";}},/** has been issued Goods * * Delivered {public string getName () {return "Shipped";}},/** received/RECEIVED {public string getName () {return "
Received ";}};
Public abstract String getName (); The public static void main (string[] args) {//enum is a type that defines a variable to limit the assignment of the variable, and the value in the enumeration is obtained by the enumeration name. Value in the assignment 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
}//Traversal enumeration System.out.println ("traversing values in Colorenum enumerations");
For (Colorenum color:ColorEnum.values ()) {System.out.println (color);
//Gets the number of enumerations System.out.println (the value in the Colorenum enumeration has the +colorenum.values (). length+ "); Gets the index position of the enumeration by default starting 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 Enumeration defaults to implement the Java.lang.Comparable interface System.out.println (ColorEnum.red.compareTo (Colorenum.green));//-1//---------
-----------------System.out.println ("===========");
System.err.println ("season for" + 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 ());
}
}
}