JDK1.5 introduces a new type--enumeration. In Java it is a "small" function, but to my development has brought "big" convenience.
Big Brother I add my own understanding, to help you understand. usage One: Constants
Before JDK1.5, we defined constants as: public static fianl ... Now that you have enumerations, you can group related constants into an enumeration type, and enumerations provide more methods than constants. Java code public enum Color {RED, GREEN, BLANK, yellow}
usage two: switch
The switch statement before JDK1.6 only supports the Int,char,enum type, and using enumerations makes 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 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 sequence of enum instances. and Java requirements must first define the enum instance. Java code public enum color { red ("Red", 1), green ("green", 2), blank ("white", 3), yello ("Yellow", 4); // member variables private String name; private int index; // Construction Method private color (string name, int index) { this.name = name; this.index = index; } // General method public static string getname (int index) {    &NBsp; for (Color c : color.values ()) { if (C.getindex () == index) { return c.name; } } return null; } // get set methods 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 of overriding enumerations
An example of a ToString () method overlay is given below. Java code public enum color { red ("Red", 1), green ("green", 2), blank ("white", 3), yello ("Yellow", 4); // member variables private String name; private int index; // Construction Method private color (string name, int index) { this.name = name; this.index = index; } //covering method @Override public string tostring () { return this.index+ "_" +this.name; } }
Usage Five: Implement Interface
All enumerations inherit from the Java.lang.Enum class. Because Java does not support