Before Java SE5, when we used enum types, we would typically use static final to define a set of int constants to identify the code as follows
Public Static Final int man = 0; Public Static Final int WOMAN = 1;
Believe that a lot of small partners, in the actual development is also so dry, since this has been able to implement the function of enumerations, why also introduce enumerations? And we're going to look down when we need to be with this set of "int enumerations" is the code below
public void showsex (int sex) { switch (Sex) { case MAN:SYSTEM.OUT.PR Intln ( This was 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, perhaps only you know that the Int type 1 represents gril,0 for boy. Other colleagues see that this function is not aware of the meaning, such code is obviously poor reading, resulting in high communication costs. We then looked down and now you have written a nice note for your func, and fools can read it (0 boy,1 gril). But the team inevitably comes up with one or two fools to send you a 3 in, and such a mistake the compiler will not report any errors, the runtime will cause a 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 usage.
Enumeration is used to store a fixed set of data sets with a limited number. Usage Scenario: The above mentioned gender, the expression of four levels a year, seven days a week, the expression of color, and so on.
Simple usage of enumerations
//Normal Enumeration Public enumcolorenum {white, red, green, blue;} /*** * The enumeration has a static method of values () that 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 * enumerations can also be added as normal classes to add properties and methods, and you can add static and non-static properties or methods to it */ Public enumSeasonenum {//Note: The enumeration is written first, otherwise the compilation errorspring, summer, autumn, winter; Private Final StaticString position = "Test"; Public Staticseasonenum Getseason () {if("Test". Equals (position))returnSpring; Else returnWinter; } } /*** Enum with constructor * must have a parameter constructor and a property and method, otherwise compilation error **/ Public enumMode {PULLDOWN ("Pull Down"), Pullup ("Pull Up"); Private FinalString value; /*** Constructors can only be private by default, ensuring that constructors are only used internally **/ PrivateMode (String value) { This. Value =value; } PublicString GetValue () {returnvalue; } }
The enumeration also has a ordinal () method that returns an int value, which is the order in which each enum instance is declared, starting at 0. The enumeration class also implements the Compareable interface, so he has the CompareTo () method. It also implements the serializable interface and automatically provides you with the Equals () and Hashcode () methods. In addition to not inheriting an enumeration class, we can basically think of an enumeration class as a regular Java class, where we can add new methods, including abstract methods and even the main method.
Now we use enumerations to implement the gender function above.
Public enumEnumsex {man, WOMAN} Public Static voidshowsex (Enumsex enumsex) {Switch(enumsex) { CaseMAN:System.out.println ("This was a boy"); Break; CaseWOMAN:System.out.println ("This is a girl"); Break; } } Public Static voidMain (string[] args) {showsex (Enumsex.man); //Showsex (Enumsex.renyao) Compile error }
This has a good reading and avoid security problems
Enumeration learning Summary of Java advanced Features