Java enum usage and javaenum usage
Original constant definition:
1 public static fianl MON=“Mon”;2 public static final TUE="Tue";
Syntax (Definition)
To create an enumeration type, use the enum keyword, which implies that all created types are subclasses of the java. lang. Enum class (java. lang. Enum is an abstract class ). The enumerated type conforms to the general mode.Class Enum<E extends Enum<E>>, AndEThe name of the enumerated type. Each value of the enumeration type is mappedprotected Enum(String name, int ordinal)In the constructor, the name of each value is converted into a string, and the ordinal number setting indicates the order in which this setting is created.
1 public enum EnumTest {2 MON, TUE, WED, THU, FRI, SAT, SUN;3 }
This Code actually calls Enum (String name, int ordinal) seven times ):
new Enum<EnumTest("MON",0);new Enum<EnumTest("TUE",1);new Enum<EnumTest("WED",2);......Common Operations such as traversal and switch
Sample Code for Traversing enum and switching:
1 public class Test {2 public static void main (String [] args) {3 for (EnumTest e: EnumTest. values () {4 System. out. println (e. toString (); 5} 6 7 System. out. println ("---------------- I am a separation line ----------------"); 8 9 EnumTest test = EnumTest. THU; 10 switch (test) {11 case MON: 12 System. out. println ("Today is Monday"); 13 break; 14 case TUE: 15 System. out. println ("Today is Tuesday"); 16 break; 17 //...... 18 default: 19 System. out. println (test); 20 break; 21} 22} 23}
Output result:Mon tue wed... ------------ I am the dividing line ------------ today is Thursday
Enum custom attributes and Methods
1 public enum EnumTest {2 2 MON ("Monday", 1), TUE ("Tuesday", 2), WED ("Wednesday", 3), THU ("THU ", 4); 3 3 // member variable 4 4 private String name; 5 5 5 private int index; 6 6 7 7 7 // constructor 8 8 private EnumTest (String name, int index) {9 9 9 this. name = name; 10 10 this. index = index; 11 11} 12 // override method 13 @ Override14 public String toString () {15 return this. index + "_" + this. name; 16} 17 12 18 13 // normal method 19 14 public static String getName (int index) {20 15 for (EnumTest e: EnumTest. values () {21 16 if (e. getIndex () = index) {22 17 return e. name; 23 18} 24 19} 25 20 return null; 26 21} 27 22 28 23 // get set Method 29 24 public String getName () {30 25 return name; 31 26} 32 27 33 28 public void setName (String name) {34 29 this. name = name; 35 30} 36 31 37 32 public int getIndex () {38 33 return index; 39 34} 40 35 41 36 public void setIndex (int index) {42 37 this. index = index; 43 38} 44 39}
1 public class Test {2 public static void main (String [] args) {3 // use of EnumSet 4 EnumSet <EnumTest> weekSet = EnumSet. allOf (EnumTest. class); 5 for (EnumTest day: weekSet) {6 System. out. println (day); 7} 8 9 // use 10 EnumMap for EnumMap <EnumTest, String> weekMap = new EnumMap (EnumTest. class); 11 weekMap. put (EnumTest. MON, "Monday"); 12 weekMap. put (EnumTest. TUE, "Tuesday"); 13 //...... 14 for (Iterator <Entry <EnumTest, String> iter = weekMap. entrySet (). iterator (); iter. hasNext ();) {15 Entry <EnumTest, String> entry = iter. next (); 16 System. out. println (entry. getKey (). name () + ":" + entry. getValue (); 17} 18} 19}Principle Analysis
Although the syntax structure of enum is different from that of class, a class file is generated after compilation by the compiler. This class file is decompiled to show that a class is actually generated, which inherits java. lang. Enum <E>. In fact, enum is a class, but the java compiler has helped us parse and compile the syntax.
Summary
We can regard enum as a common class, which can define some attributes and methods. The difference is that enum cannot use the extends keyword to inherit other classes, because enum has inherited java. lang. enum (java is a single inheritance ).