*5, Enum 其實實現了 Comparable interface,能夠直接使用 Comparable 進行排序。java
Colorthis
package com.qunar.basicJava.javase.p14enum; /** * Author: libin.chen@qunar.com Date: 14-6-11 17:23 */ public enum Color { RED("紅色"), GREEN("綠色"), BLUE("藍色"); private String name; Color(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }Main
package com.qunar.basicJava.javase.p14enum; /** * Author: libin.chen@qunar.com Date: 14-6-11 17:28 */ public class ConstructorEnum { public static void main(String[] args) { for (Color c : Color.values()) { System.out.println(c.ordinal() + " --> " + c.name() + "(" + c.getName() + ")"); } } }
輸出 :code
0 --> RED(紅色)
1 --> GREEN(綠色)
2 --> BLUE(藍色)
對象