What is an enumeration?
Enumeration is a new type introduced by JDK1.5. Defining an enumeration type is essentially defining a category, except that a lot of detail is done by the compiler, so that, to some extent, the enum keyword acts like a class or interface
When we use "enum" to define an enumeration type, essentially the type we define is inherited from the Java.lang.Enum type, and the member of the enumeration is actually an instance of the enumeration type we define (Instance), and they are all preset to final, so we can't change them, They are also static members, so we can use them directly by type name, and of course most importantly, they are public.
That is, each enumerated type that we define inherits from the Java.lang.Enum class, and each member of the enumeration is public static final by default.
The member of each enumeration is actually an instance of the enumeration type that we define (Instance). In other words, when an enumeration type is defined, at compile time it can be determined that there are several instances of the enumeration type, and what the difference is. We are no longer able to use this enumeration type to create new instances during runtime, and these instances have been fully identified during compilation. So we can use this: Color MyColor = color.red, not new Color (), but direct assignment
How to use it?
Usage One: Constants
Before JDK1.5, we defined constants: public static fianl ..... Now, with enumerations, you can group related constants into an enumeration type, and enumerations provide more methods than constants.
public enum Color { RED, GREEN, BLANK, YELLOW}
Usage Two: Switch
The switch statement before JDK1.6 only supports int,char,enum types, and using enumerations can make our code more readable.
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 Three: Add a new method to the enumeration
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 enum Color ( "Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); Member variable private String name; private int index; Construction Method Private Color (String name, int index) { this.name = name; This.index = index; } Normal method public static String getName (int index) {for (Color c:color.values ()) { if (c.getindex () = = Inde x) { 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 Four: Methods for overriding enumerations
An example of the ToString () method overlay is given below.
public enum Color ( "Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); Member variable private String name; private int index; Construction Method Private Color (String name, int index) { this.name = name; This.index = index; } overriding method @Override public String toString () { return this.index+ "_" +this.name; }}
Usage Five: Implement Interface
All enumerations are inherited from the Java.lang.Enum class. Because Java does not support multiple inheritance, enumeration objects can no longer inherit from other classes.
Public interface Behaviour { void print (); String getInfo ();} Public enum Color implements behaviour{red ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); Member variable private String name; private int index; Construction Method 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 VI: Organizing enumerations using interfaces
Public interface Food { enum Coffee implements food{ Black_coffee,decaf_coffee,latte,cappuccino } enum Dessert implements food{ FRUIT, CAKE, GELATO }}
Usage Seven: About the use of enumeration collections
Java.util.EnumSet and Java.util.EnumMap are two enumeration collections. Enumset guarantees that the elements in the collection are not duplicated; The key in Enummap is the enum type, and value can be any type. The use of this two collection is not mentioned here, you can refer to the JDK documentation.
Note: For the implementation details and principles of enumerations, refer to: Reference: Thinking in Java, fourth edition
Part of the content from: http://blog.jobbole.com/31597/
--The impermanence, the widow and the Wild goose
--2015-10-06
Use of enumerations