Before Java SE5, we would typically use the static final to define a set of int constants to identify when using enum types. Code such as the following
publicstaticfinalint0;publicstaticfinalint1;
Believe that very many small partners, in the actual development is also so dry. Now that you have the ability to implement enumerations, why do you introduce enumerations? And we'll look down when we need to be with this group of "int enumerations" is code such as the following
publicvoidshowSex(int sex){ switch(sex){ case MAN: System.out.println("this is a boy"); break; case WOMAN: System.out.println("this is a Girl"); break; } }
It seems like there's nothing wrong with this, but we know that a project is basically based on team development. Maybe only you know that 1 of the int type represents gril,0 for boy.
Other colleagues saw that the function did not know what the meaning was at all. This kind of code is obviously very poor in reading. This can result in very high communication costs.
We went on to look down. Now you have written a very nice gaze for your func, and fools can read it (0 boy,1 gril).
But the project team inevitably will appear so one or two fools, not to send you a 3 come in, and this error compiler will not report no matter what error, the implementation of the cause of the bug, this is only Joe Master know. So this code is extremely insecure.
The use of enumerations can be very good to avoid the above problems, and then we will take a rationale for enumeration of the use of methods.
Enumeration is used to store a fixed set of data sets with a limited number.
Usage Scenario: The expression of the gender mentioned above. Year Four seasons, the expression of seven days a week, the expression of color, and so on.
Simple ways to use enumerations
//Normal enumeration Public enumColorenum {white, red, green, blue;}/** * * The enumeration has a self-contained static method, values (), which returns the data of the enum instance and the order of the elements in the array is the same as the order in which it was declared * The enumeration is also capable of adding properties and methods as normal classes. Ability to add static and non-static properties or methods to it */ Public enumSeasonenum {//Note: Enumeration is written at the top, otherwise compilation errorSpring, summer, autumn, winter;Private Final StaticString position ="Test"; Public StaticSeasonenumGetseason() {if("Test". Equals (position))returnSpringElse returnWinter } }/** * With Constructor enumeration * must have a constructor and a property followed by a method, otherwise the compilation error * */ Public enumMode {PULLDOWN ("drop Down"), Pullup ("Pull Up");Private FinalString value;The /** * constructor can only be private by default, guaranteeing that the constructor can only be used internally * */ Private Mode(String value) { This. value = value; } PublicStringGetValue() {returnValue } }
Another ordinal () method in the enumeration returns an int value, which is the order in which each instance of an enum is declared, starting at 0. The enumeration class also implements the Compareable interface, so he has the CompareTo () method.
The serializable interface is also implemented at the same time. You are also offered the Equals () and Hashcode () methods on your own initiative.
In addition to being unable to inherit an enumeration class, we can basically think of an enumeration class as a regular Java class, with the ability to add new methods to it, including abstract methods and even the main method.
Now we use enumerations to implement the gender function above.
Public enumEnumsex {man, WOMAN} Public Static void Showsex(Enumsex enumsex) {Switch(Enumsex) { CaseMan:system. out. println (" This was a boy"); Break; CaseWoman:system. out. println ("This is a girl"); Break; } } Public Static void Main(string[] args) {showsex (Enumsex.man);//Showsex (Enumsex.renyao) Compile error}
This has a very good reading and avoid security issues
Enumeration of Java advanced features