Usage 1: Constant
Before jdk1.5, we define constants as publicstaticfianl ..... Now, with enumeration, You can group related constants into one Enumeration type, and enumeration provides more methods than constants.
Java code
- Public Enum color {
- Red, green, blank, yellow
- }
Usage 2: Switch
The switch statement before jdk1.6 only supports int, Char, and enum types, and uses enumeration to make our code more readable.
Java code
- Enum signal {
- Green, yellow, red
- }
- Public class trafficlight {
- Signal color = signal. Red;
- Public void change (){
- Switch (color ){
- Case Red:
- Color = signal. Green;
- Break;
- Case Yellow:
- Color = signal. Red;
- Break;
- Case Green:
- Color = signal. Yellow;
- Break;
- }
- }
- }
Usage 3: Add a new method to Enumeration
If you want to customize your own method, you must add a semicolon at the end of the enum instance sequence. In addition, Java requires that the enum instance be defined first.
Java code
- Public Enum color {
- Red ("red", 1), Green ("green", 2 ),
Blank ("white", 3), yello ("yellow", 4 );
- // Member variable
- Private string name;
- Private int index;
- // Constructor
- Private color (string name,
Int index ){
- This. Name = Name;
- This. Index = index;
- }
- // Common method
- Public static string getname (INT index ){
- For (color C: color. Values ()){
- If (C. getindex () = index ){
- Return C. Name;
- }
- }
- Return NULL;
- }
- // Get Set Method
- Public String getname (){
- Return name;
- }
- Public void setname (string name ){
- This. Name = Name;
- }
- Public int getindex (){
- Return Index;
- }
- Public void setindex (INT index ){
- This. Index = index;
- }
- }
Usage 4: override Enumeration Method
The following is an example of overwriting the tostring () method.
Java code
- Public Enum color {
- Red ("red", 1), Green ("green", 2 ),
Blank ("white", 3), yello ("yellow", 4 );
- // Member variable
- Private string name;
- Private int index;
- // Constructor
- Private color (string name,
Int index ){
- This. Name = Name;
- This. Index = index;
- }
- // Override method
- @ Override
- Public String tostring (){
- Return this. index + "_" + this. Name;
- }
- }
Usage 5: Implementation Interface
All enumerations are inherited from the java. Lang. Enum class. Because Java does not support multi-inheritance, enumeration objects cannot inherit other classes.
Java code
- Public Interface Behaviour {
- Void print ();
- String getinfo ();
- }
- Public Enum colorimplements behaviour {
- Red ("red", 1), Green ("green", 2 ),
Blank ("white", 3), yello ("yellow", 4 );
- // Member variable
- Private string name;
- Private int index;
- // Constructor
- Private color (string name,
Int index ){
- This. Name = Name;
- This. Index = index;
- }
- // Interface Method
- @ Override
- Public String getinfo (){
- Return this. Name;
- }
- // Interface Method
- @ Override
- Public void print (){
- System. Out. println (this. index + ":" + this. Name );
- }
- }
Usage 6: organize enumeration Using Interfaces
Java code
- Public interface food {
- Enum coffee implements food {
- Black_coffee, decaf_coffee, latte, cappuccino
- }
- Enum dessert implements food {
- Fruit, cake, gelato
- }
- }
Usage 7: Use of enumeration Sets
Java. util. enumset and Java. util. enummap are two enumeration sets. Enumset ensures that the elements in the set are not repeated. The key in enummap is of the enum type, and the value can be of any type. The usage of these two sets is not described here. You can refer to the JDK documentation.
For the implementation details and principles of enumeration, refer:
Reference: thinkinginjava version 4
Http://softbeta.iteye.com/blog/1185573
Java enumeration can be used in combination with jstl labels and hibernate technologies. The effect of Java enumeration is greatly enhanced, and the code is clear and maintainability is high.
Reprint please indicate the source http://blog.csdn.net/shimiso
Technical Exchange Group: 173711587