I. Using enumerations to represent data dictionaries
1. Code:
Packagecom.inspire.jdk.caculate;/*** Created by yaming * Use enumerations to represent constant data fields*/ Public enumStateenum {
// using construction methods to create objects , each of which is an instance of stateenum SUCCESS (1, "Success"), FAILED (0, "failed"); Private intState ; PrivateString StateInfo; Public intgetState () {returnState ; } PublicString Getstateinfo () {returnStateInfo; }Stateenum (intState , String StateInfo) { This. State =State ; This. StateInfo =StateInfo; } /*** Find corresponding Stateenum according to index of INT type *@paramIndex *@return */ Public StaticStateenum stateof (intindex) { for(Stateenum state:values ()) {if(state.getstate () = =index) { returnState ; } } return NULL; } /*** Find the value of the corresponding string according to the index of type int *@paramIndex *@return */ Public StaticString Valuesof (intindex) { for(Stateenum state:values ()) {if(state.getstate () = =index) { returnState.getstateinfo (); } } return"NULL"; }}
The enumeration class (enum) in 2.java is the same as the normal class (class) in Java, and only a series of sample variables are pre-defined
such as: Public enum Color {// each variable is a sample of color RED, YELLOW, BLUE;}
2.1 It is possible to write sample variables, such as Red,yellow,blue, directly within the enumeration.
The 2.2 enumeration has some naturally obtained member methods, the Color.values () method
2.3 An enumeration class is similar to a normal class on the basis that an enumeration can also contain constructors
3. Summary:
Enumeration limits the number and likelihood of instance variables of that type, because an instance variable of an enumeration determines when the enumeration is defined
Using enumerations to represent data dictionaries in Java development