原文來自搬磚工,如需轉載請注明出處
這篇文章從認識枚舉、枚舉詳解到枚舉使用舉例來總結java枚舉的相干知識
1、認識枚舉
枚舉類型是Java SE 5.0 以后的版本新定義出來的,我們先來簡單看1個枚舉的定義:
public enum Color{
RED,BLUE,BLACK,YELLOW,GREEN
}
明顯,enum很像特殊的class類,實際上enum聲明定義的類型就是1個類。 而這些類都是類庫中Enum類的子類(Java.lang.Enum)。它們繼承了這個Enum中的許多有用的方法。我們對代碼編譯以后發現,編譯器將 enum類型單獨編譯成了1個字節碼文件——Color.class
文件內容:
final enum hr.test.Color { // 所有的枚舉值都是類靜態常量 public static final enum hr.test.Color RED; public static final enum hr.test.Color BLUE; public static final enum hr.test.Color BLACK; public static final enum hr.test.Color YELLOW; public static final enum hr.test.Color GREEN; private static final synthetic hr.test.Color[] ENUM$VALUES; }2、枚舉詳解
下面以Color類為例總結1下枚舉的相干知識:
1. Color枚舉類就是class,而且是1個不可以被繼承的final類。其枚舉值(RED,BLUE......)都是Color類型的類靜態常量, 我們可以通過下面的方式來得到Color枚舉類的1個實例:
Color c=Color.RED;注意:這些枚舉值都是public static final的,也就是我們常常所定義的常量方式,因此枚舉類中的枚舉值最好全部大寫。
2. 即然枚舉類是class,固然在枚舉類型中有構造器,方法和數據域。但是,枚舉類的構造器有很大的不同:
(1)構造器只是在構造枚舉值的時候被調用
enum Color{ RED(255,0,0),BLUE(0,0,255),BLACK(0,0,0),YELLOW(255,255,0),GREEN(0,255,0); //構造枚舉值,比如RED(255,0,0) private Color(int rv,int gv,int bv){ this.redValue=rv; this.greenValue=gv; this.blueValue=bv; } public String toString(){ //覆蓋了父類Enum的toString() return super.toString()+“(”+redValue+“,”+greenValue+“,”+blueValue+“)”; } private int redValue; //自定義數據域,private為了封裝。 private int greenValue; private int blueValue; }(2) 構造器只能私有private,絕對不允許有public構造器。 這樣可以保證外部代碼沒法新構造枚舉類的實例。這也是完全符合情理的,由于我們知道枚舉值是public static final的常量而已。 但枚舉類的方法和數據域可以允許外部訪問。
public static void main(String args[]){ // Color colors=new Color(100,200,300); //wrong Color color=Color.RED; System.out.println(color); // 調用了toString()方法 }3. 所有枚舉類都繼承了Enum的方法,下面我們詳細介紹這些方法:
(1)ordinal()方法: 返回枚舉值在枚舉類種的順序。這個順序根據枚舉值聲明的順序而定。
Color.RED.ordinal(); //返回結果:0 Color.BLUE.ordinal(); //返回結果:1(2)compareTo()方法: Enum實現了java.lang.Comparable接口,因此可以比較象與指定對象的順序。Enum中的compareTo返回的是兩個枚舉值的順 序之差。固然,條件是兩個枚舉值必須屬于同1個枚舉類,否則會拋出ClassCastException()異常。(具體可見源代碼)
Color.RED.compareTo(Color.BLUE); //返回結果 ⑴(3)values()方法: 靜態方法,返回1個包括全部枚舉值的數組:
Color [] colors=Color.values(); for(Color c:colors){ System.out.print(c+“,”); }//返回結果:RED,BLUE,BLACK YELLOW,GREEN,(4)toString()方法: 返回枚舉常量的名稱
Color c=Color.RED; System.out.println(c);//返回結果: RED(5)valueOf()方法: 這個方法和toString方法是相對應的,返回帶指定名稱的指定枚舉類型的枚舉常量。
Color.valueOf(“BLUE”); //返回結果: Color.BLUE(6)equals()方法: 比較兩個枚舉類對象的援用。
public final boolean equals(Object other) { return this==other; }4. 枚舉類可以在switch語句中使用。
Color color=Color.RED; switch(color){ case RED: System.out.println("it‘s red");break; case BLUE: System.out.println("it’s blue");break; case BLACK: System.out.println("it‘s blue");break; }3、枚舉的具體使用
用法1:常量
在JDK1.5 之前,我們定義常量都是: publicstaticfianl.... 。現在好了,有了枚舉,可以把相干的常量分組到1個枚舉類型里,而且枚舉提供了比常量更多的方法。
public enum Color {
RED, GREEN, BLANK, YELLOW
}
用法2:switch
JDK1.6之前的switch語句只支持int,char,enum類型,使用枚舉,能讓我們的代碼可讀性更強。
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; } } }用法3:向枚舉中添加新方法
public enum Color { RED("紅色", 1), GREEN("綠色", 2), BLANK("白色", 3), YELLO("黃色", 4); // 成員變量 private String name; private int index; // 構造方法 private Color(String name, int index) { this.name = name; this.index = index; } // 普通方法 public static String getName(int index) { for (Color c : Color.values()) { if (c.getIndex() == index) { return c.name; } } return null; } // get set 方法 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; } }用法4:覆蓋枚舉的方法
public enum Color { RED("紅色", 1), GREEN("綠色", 2), BLANK("白色", 3), YELLO("黃色", 4); // 成員變量 private String name; private int index; // 構造方法 private Color(String name, int index) { this.name = name; this.index = index; } //覆蓋方法 @Override public String toString() { return this.index+"_"+this.name; } }用法5:實現接口
public interface Behaviour { void print(); String getInfo(); } public enum Color implements Behaviour{ RED("紅色", 1), GREEN("綠色", 2), BLANK("白色", 3), YELLO("黃色", 4); // 成員變量 private String name; private int index; // 構造方法 private Color(String name, int index) { this.name = name; this.index = index; } //接口方法 @Override public String getInfo() { return this.name; } //接口方法 @Override public void print() { System.out.println(this.index+":"+this.name); } }用法6:使用接口組織枚舉
public interface Food { enum Coffee implements Food{ BLACK_COFFEE,DECAF_COFFEE,LATTE,CAPPUCCINO } enum Dessert implements Food{ FRUIT, CAKE, GELATO } }用法7:關于枚舉集合的使用
4、替換枚舉的常見方法
class WeekDay{ public static final WeekDay SUN = new WeekDay(); public static final WeekDay MON = new WeekDay(); public static final WeekDay TUE = new WeekDay(); public static final WeekDay WED = new WeekDay(); public static final WeekDay THU = new WeekDay(); public static final WeekDay FRI = new WeekDay(); public static final WeekDay SAT = new WeekDay(); public String toString(){ if(this==SUN) return "SUN"; else if (this==MON) return "MON"; else if (this==TUE) return "TUE"; else if (this==WED) return "WED"; else if (this==THU) return "THU"; else if (this==FRI) return "FRI"; else return "SAT"; } public static void main(String [] args){ WeekDay weekDay = WeekDay.SUN; System.out.println(weekDay.toString()); } }5、小結
1.enum類型可以放在switch語句中進行判斷。
2.String toString() 返回枚舉常量名
3.int ordinal() 返回枚舉常量在enum聲明中的位置 位置從0開始計數
4.int compareTo(E other) 如果枚舉常量出現在other之前,則返回1個負值;如果this==other ,返回0,否則返回1個正值
5.Size[] values = Size.values(); enum類有1個values的方法, 可以返回1個enum類型的數組,也能夠用enum.values().length() 取得枚舉類型變量的長度。
上一篇 jsp的taglib指令用法
下一篇 小程序,會是下一個創業風口嗎