DK 1.5 introduces a new type, enumeration. Although it is a "small" function in Java, it brings "big" convenience to my development.
Usage 1: Constants
Before JDK 1.5, we defined constants as: publicstaticfianl.... Now, with enumeration, you can group related constants into an enumeration type, and enumeration provides more methods than constants.
- public enum Color {
- RED, GREEN, BLANK, YELLOW
- }
Usage 2:switch
The switch statements before JDK 1.6 only support int,char,enum types. Enumeration 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 3: Add a new method to the enumeration
If you want to customize your method, you must add a semicolon at the end of the Enum instance sequence. Furthermore, Java requires that enum instances must be defined first.
- public enum Color {
- RED("gules", 1), GREEN("green", 2), BLANK("white", 3), YELLO("yellow", 4);
- //Membership variables
- private String name;
- private int index;
- //Construction method
- private Color(String name, int index) {
- this.name = name;
- this.index = index;
- }
- //General methods
- 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: Overlay Enumeration Method
An example of toString() method coverage is given below.
- public enum Color {
- RED("gules", 1), GREEN("green", 2), BLANK("white", 3), YELLO("yellow", 4);
- //Membership variables
- private String name;
- private int index;
- //Construction method
- private Color(String name, int index) {
- this.name = name;
- this.index = index;
- }
- //Coverage method
- @Override
- public String toString() {
- return this.index+"_"+this.name;
- }
- }
Usage 5: Implementing interfaces
All enumerations inherit from the java.lang.Enum class. Since Java does not support multiple inheritance, enumerated objects can no longer inherit other classes.
- public interface Behaviour {
- void print();
- String getInfo();
- }
- public enum Color implements Behaviour{
- RED("gules", 1), GREEN("green", 2), BLANK("white", 3), YELLO("yellow", 4);
- //Membership variables
- 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 6: Organize enumerations using interfaces
- public interface Food {
- enum Coffee implements Food{
- BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO
- }
- enum Dessert implements Food{
- FRUIT, CAKE, GELATO
- }
- }
Usage 7: About the Use of Enumerated Sets
java.util.EnumSet and java.util.EnumMap are two sets of enumerations. EnumSet guarantees that elements in a collection are not duplicated; key s in EnumMap are enum types and value s can be arbitrary types. The use of these two sets is not covered here, you can refer to the JDK documentation.
For details and principles of enumeration, please refer to:
Reference: Thinking In Java, 4th Edition
http://softbeta.iteye.com/blog/1185573