JDK1.5 introduces a new type--enumeration. Although it is a "small" function in Java, it is very helpful for development.
Related uses of enumerations
Usage One:
Before JDK1.5, we defined constants: public static fianl ..... Well now, with the enumeration, you can put
Related constants are grouped into an enumeration type, and enumerations provide more methods than constants.
Public enum Color { RED, GREEN, BLANK, YELLOW
Usage Two:
The switch statement before JDK1.6 only supports int,char,enum types, and using enumerations can make our code more readable.
enumSignal {GREEN, YELLOW, RED} Public classtrafficlight {Signal color=signal.red; Public voidChange () {Switch(color) { CaseRed:color=Signal.green; Break; CaseYellow:color=signal.red; Break; CaseGreen:color=Signal.yellow; Break; } } }
Usage Three:
If you intend to customize your own method, you must add a semicolon at the end of the enum instance sequence. and Java
Requires that an enum instance be defined first.
Public enumColor {RED ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); //member Variables PrivateString name; Private intindex; //Construction Method PrivateColor (String name,intindex) { This. Name =name; This. index =index; } //Common Methods Public StaticString GetName (intindex) { for(Color c:color.values ()) {if(C.getindex () = =index) { returnC.name; } } return NULL; } //Get Set Method PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetIndex () {returnindex; } Public voidSetindex (intindex) { This. index =index; } }
For more information on the use of enums:
http://blog.lichengwu.cn/java/2011/09/26/the-usage-of-enum-in-java/
52180865
Specifications that are recommended when using an enum
1. Enumeration class names are suggested with an enum suffix, and enumeration member names need to be all uppercase, and the words are separated by underscores.
Description: The enumeration is a special constant class, and the construction method is forced to private by default.
Example:
Enumeration Name: Dealstatusenum
Member Name: Success/unkown_reason
2. All enum type fields must have a comment indicating the purpose of each data item
3, the enumeration type objects between the value comparison, you can use = =, directly to compare the value is equal, not must use the Equals method
4. The constructor of an enumeration class can only be private.
Application of Java enumeration type