In actual programming, there are often such "data sets". Their values are stable in the program, and the elements in "data sets" are limited.
For example, from Monday to Sunday, the seven data elements formed a week's "data set", and the four data elements of spring, summer, autumn, and winter form a "data set" of four seasons.
How to better use these "data sets" in Java? Therefore, the enumeration comes in hand, and the following code introduces the enumeration usage in detail.
package com.ljq.test;/** * enumeration method detailed explanation * * @AutHor jiqinlin * */Public Class Testenum {/** * ordinary enumeration * * @author jiqinlin */public enum colorNum {Red, Green, YELLOW, Blue;} /** * enumeration like an ordinary class can add attributes and methods, you can add static and non -static attributes or methods * * * @Author jiqinlin * * /Public Enum Seasonum {// Note: The enumeration is written in the front, otherwise compiles the wrong Spring, Summer, Autumn, Winter; Private Final Static String Position = "Test"; pose) Return Spring; Else Return Winter;}} /** * Gender * * Implementation of enumeration with a constructor * * * @Author jiqinlin * * /Public Enum Gender {// It must be assigned by brackets, and it must bring a constructor and a attribute and a follower. Methods, otherwise the compilation of errors // The assignment must be assigned or not assigned, and part of the assignment must not be assigned; if the value is not assigned, the constructor cannot be written. Private final string value; // The constructor can only be private by default, thereby ensuring that the constructor can only use the Gender (String Value) {this.Value = value;} Public String GetValue () { rn value;}} / *** Order status** Metic enumeration with abstract methods*** @Author jiqinlin**/ Public Enum orderstate {/ ** has been canceled*/ cannot {public string getName () {Return ";}}}} , / ** to be reviewed* / waitConfirm {public string getname () {Return "to be reviewed";}}, / ** Waiting for payment* / waitPayment {Public String GetName () {RetURN "Waiting for payment" ;}}, / ** Dating*/ admeasureproduct {public string getName () {Return "is distribution";}},/ ** wait for delivery*/ waitdeliver {public string getname () {"waiting for delivery" ;}} , / ** Shipped* / Delivered {Public String GetName () {Return ";}}, / ** Received* / Received {Public String GetName () {Return" has been received "; }}; Public Abstract String GetName ();} Public Static Void Main (String [] Args) {// enumeration is a type for defining variables to limit the assignment of variables; when assigning, "enumeration name. "Value" Get the enumeration value colornum colornum = colornum.blue; switch (colorNum) {case red: system.out.println ("color is red"); BREAK; Case Green: system.ououou T.println ("COLOR is Green "); Bream; Case YELLOW: System.out.println (" Color is Yellow "); Break; Case Blue: System.out.println (" COLOR Is Blue "); Break; em. Out.println ("Elves the value in the colorNum enumeration"); for (colornum color: colornum.values ()) {system.out.println (color);} // Get the number of enumeration systers.out.println ("Colorenum's enumeration value is"+Colorenum.Values (). LENGTH+""); // Get the indexing position of the enumeration, and start from 0 from 0 systerm.out.println (colornum.red.ord.ordinal ())) ;//0 System.out.println(ColorEnum.green.ordinal());//1 System.out.println(ColorEnum.yellow.ordinal());//2 System.out.println(ColorEnum.blue. Ordinal ()); // 3 // enumerates java.lang.comparable interface system.out.println (colornum.red.compareto (colorNum.green)); //-1/-------- --------------------- System.out.println("==========="); System.err.println("季节为" + SeasonEnum.getSeason()); //-------------- System.out.println("==========="); for(Gender Gender: Gender.Values ()) {system.out.println (gender.Value);} // ----------------------------------------------------------------------------------- ===== "); for (orderState Order: Ordestate.Values ()) {System.out.println (order.getName ());}}}