Emum is defined as follows:
Public Abstract class Enum<e extends Enum<e>> extends comparable Serializable
This class definition uses a generic interface and implements the comparable interface and the Serializable interface, proving that this type can be compared and serialized .
Construction Method:
protected int ordinal)
Emun constructs a method that receives two parameters, one that represents the name of the enumeration (name ()), and another that represents the ordinal ordinal ()of the enumeration.
Public enum color{ red,green,blue;}
Red is actually the name of the enumeration, and the default number is 0.
Example:
Package class set; enum color1{Red,black,green }publicclass test1{ public staticvoid main (String args[]) { for(Color1 c:color1.values ()) { System.out.println (c.+ "-+" + C.name ()) ; }}}
Results:
0--- Red1-- Black2---Green
If you want to make some improvements at this point and want to use text to represent the color information , you can define the attribute (that is, name) and the constructor method in the enumeration class as a color class.
However , once the argument is declared, the constructor must be explicitly called when the enumeration object is declared, and parameters are passed .
As follows:
Packageclass set;enumcolor{RED ("Red"), GREEN ("Green"), BLUE ("Blue color") ; //Explicitly call the constructor method, and pass the parameters. Private Color (String name){ This. SetName (name); } private String name; Define the Name property Public voidsetName (String name) { This. Name =name; } PublicString getName (){ return This. Name; }} Public classtest1{ Public Static voidMain (String args[]) { for(Color c:color.values ()) {System.out.println (c.ordinal() )+ "-+" +c.name()+ "(" + C. getName()+ ")") ; } }}
return Result:
0- -red1- --Green2---Blue (blue)
If you do not want to set the content by constructing the method now, but want to set the content through the getter (), setter () method, you must do so in the following manner.
Packageclass set;enumcolor{ red,green,blue; private String name; Define the Name property Public void setName(String name) {Switch( This){//The enumeration object that determines the operation Case red:{ if ("Red" . Equals (name)) {// must also determine if the content of the setting is qualified. This. name = name;//allow setting the name}Else{System.out.println ("Error setting content. ") ; } Break ; } Casegreen:{if("Green". Equals (name)) { This. name = name;//allow setting the name}Else{System.out.println ("Error setting content. ") ; } Break ; } Caseblue:{if("Blue". Equals (name)) { This. name = name;//allow setting the name}Else{System.out.println ("Error setting content. ") ; } Break ; } } This. Name =name; } PublicString getName (){return This. Name; }} Public classtest1{ Public Static voidMain (String args[]) {Color C= Color.BLUE;//get the blue colorC.setname ("Blue color") ;//Wrong nameC.setname ("Blue");//right name .System.out.println (C.getname ()); }}
Output Result:
Error setting content. Blue
The above findings are easier to construct by means of construction.
The enumeration constants are obtained by specifying the enumeration name through the valueof () method
Static extends enum<t>> T valueOf (class<t>String name)
You can replace the first sentence in the main method of the above code. As follows.
Packageclass set;enumcolor{Red,green,blue; private String name; Define the Name property Public voidsetName (String name) {Switch( This){//The enumeration object that determines the operation Casered:{if("Red". Equals (name)) { This. name = name;//allow setting the name}Else{System.out.println ("Error setting content. ") ; } Break ; } Casegreen:{if("Green". Equals (name)) { This. name = name;//allow setting the name}Else{System.out.println ("Error setting content. ") ; } Break ; } Caseblue:{if("Blue". Equals (name)) { This. name = name;//allow setting the name}Else{System.out.println ("Error setting content. ") ; } Break ; } } This. Name =name; } PublicString GetName () {return This. Name; }} Public classtest1{ Public Static voidMain (String args[]) {Color C= Color.valueof (Color.class, "BLUE");//get the blue colorC.setname ("Blue");//Wrong nameC.setname ("Blue");//right name .System.out.println (C.getname ()); }}
For enumerations. Class is the reflection mechanism content in Java.
The comparable interface is actually implemented in the enumeration, so the contents of the enumeration can be sorted.
Packageclass set;ImportJava.util.Iterator;ImportJava.util.Set;ImportJava.util.TreeSet;enumcolor{Red,green,blue;} Public classtest1{ Public Static voidMain (String args[]) { Set <Color> t = new treeset<color> (); Set TypeT.add (Color.green);//Add GreenT.add (color.red);//Add RedT.add (Color.Blue);//Add BlueIterator<color> iter =T.iterator (); while(Iter.hasnext ()) {System.out.print (Iter.next () )+ ",") ; } }}
Output
RED, GREEN, BLUE,
It can be found that the time to join the TreeSet is unordered and the output is orderly.
by TreeSet
Emum Class (2)