The original constant definition:
1 Public static fianl mon="MON"; 2 Public Static Final Tue= "TUE";
syntax (definition)
Creating an enumeration type to use the enum keyword implies that the type created is a subclass of the Java.lang.Enum class (Java.lang.Enum is an abstract class). An enumeration type conforms to a common pattern and Class Enum<E extends Enum<E>> E represents the name of an enumeration type. Each value of the enumeration type is mapped to the protected Enum(String name, int ordinal) constructor, where the name of each value is converted to a string, and the ordinal setting indicates the order in which this setting was created.
1 Public enum enumtest {2 MON, TUE, WED, THU, FRI, SAT, SUN; 3 }
This code actually calls the Enum 7 times (String name, int ordinal):
New Enum<enumtest ("MON", 0); New Enum<enumtest ("TUE", 1); New Enum<enumtest ("WED", 2);
traversal, switch and other common operations
Example code for iterating through and switch to an enum:
1 Public classTest {2 Public Static voidMain (string[] args) {3 for(Enumtest e:enumtest.values ()) {4 System.out.println (e.tostring ());5 }6 7System.out.println ("----------------I am the dividing line------------------");8 9Enumtest test =Enumtest.thu;Ten Switch(test) { One CaseMON: ASystem.out.println ("Today is Monday"); - Break; - CaseTUE: theSystem.out.println ("Today is Tuesday"); - Break; - // ... ... - default: + System.out.println (test); - Break; + } A } at}
Output Result:MON TUE WED ...------------I'm a split line--------------today is Thursday
enum Custom properties and methods
1 Public enumenumtest{22 MON ("Monday", 1), TUE ("Tuesday", 2), WED ("Wednesday", 3), THU ("Thursday", 4);33//member Variables44PrivateString name;55Private intindex;6677//Construction Method88PrivateEnumtest (String name,intindex) {99 This. Name =name;Ten10 This. index =index; One11 } A //override Method - @Override - PublicString toString () { the return This. Index + "_" + This. Name; - } -12 -13//Common Methods +14 Public StaticString GetName (intindex) { -15 for(Enumtest e:enumtest.values ()) { +16if(E.getindex () = =index) { A17returne.name; at18 } -19 } -20return NULL; -21st } -22 -23//Get Set Method in24 PublicString GetName () { -25returnname; to26 } +27 -28 Public voidsetName (String name) { the29 This. Name =name; *30 } $31Panax Notoginseng32 Public intGetIndex () { -33returnindex; the34 } +35 A36 Public voidSetindex (intindex) { the37 This. index =index; +38 } -39}
1 Public classTest {2 Public Static voidMain (string[] args) {3 //Use of Enumset4enumset<enumtest> Weekset = Enumset.allof (enumtest.class);5 for(Enumtest day:weekset) {6 System.out.println (day);7 }8 9 //Use of EnummapTenEnummap<enumtest, string> weekmap =NewEnummap (enumtest.class); OneWeekmap.put (Enumtest.mon, "Monday"); AWeekmap.put (Enumtest.tue, "Tuesday"); - // ... ... - for(Iterator<entry<enumtest, string>> iter =Weekmap.entryset (). iterator (); Iter.hasnext ();) { theEntry<enumtest, string> Entry =Iter.next (); -System.out.println (Entry.getkey (). Name () + ":" +Entry.getvalue ()); - } - } +}Principle Analysis
The syntax structure of an enum is not the same as the syntax of class, but a class file is generated after the compiler compiles. The class file is de-compiled to see that a class has actually been generated that inherits the java.lang.enum<e>. In fact, an enum is a class, but the Java compiler helps us to parse and compile the syntax.
Summary
You can think of an enum as a normal class, and they all define properties and methods, but the difference is that enums cannot inherit other classes using the extends keyword, because enum inherits Java.lang.Enum (Java is a single inheritance).
Usage of Java enum