深入掌握Java中的enum

來源:互聯網
上載者:User

標籤:

對於要在程式中要表示有限種類的某事物,一般我們可以採用兩種方式,一是使用:public static final String 常量;二是使用enum來表示。一般而言前者簡單,但是不能夠很好的提供更多的資訊,而Java中的enum相比而言,卻十分的強大,而且更加的專業。

1. 最間C風格的enum:

/** * 資料來源的類別:master/slave */public enum DataSources {    MASTER0, MASTER1, SLAVE0, SLAVE1, SLAVE2,  SLAVE2}

這是最簡單的enum, 和C語言中的幾乎一樣。簡單簡潔但是功能也很弱。

2. enum 的本質

Java中的enum的本質是一個繼承java.lang.Enum的類。所以他就比C風格的enum更加的強大。它可以又屬性,方法,建構函式等等,下面看一個例子:

import org.apache.commons.lang.StringUtils;/** * 錯誤碼枚舉*/public enum ErrorCodeEnum {    /** 系統異常 */    SYSTEM_ERROR("system_error", "系統異常"),    /** 參數非法 */    ILLEGAL_ARGUMENT("illegal_argument", "參數非法"),    /** 簽名非法 */    ILLEGAL_SIGN("illegal_sign", "簽名非法"),

// ... ...
/** 註冊碼非法 */ ILLEGAL_REG_CODE("illegal_reg_code", "註冊碼非法"); /** 枚舉碼 */ private String code; /** 枚舉描述 */ private String desc; private ErrorCodeEnum(String code, String desc) { this.code = code; this.desc = desc; } public String getCode() { return code; } public String getDesc() { return desc; } /** * 根據枚舉碼擷取枚舉 * @param code 枚舉碼 * @return 枚舉 */ public static final ErrorCodeEnum getByCode(String code) { if (StringUtils.isBlank(code)) { return null; } for (ErrorCodeEnum item : ErrorCodeEnum.values()) { if (StringUtils.equals(item.getCode(), code)) { return item; } } return null; }}

我們看到 ErrorCodeEnum  具有屬性 String code 和 String desc,並且具有一個私人的建構函式。原因是我們的枚舉常量需要使用這個私人的建構函式的定義:SYSTEM_ERROR("system_error", "系統異常") 就是調用的枚舉的私人建構函式:private ErrorCodeEnum(String code, String desc);所以其實ErrorCodeEnum 中定義的枚舉常量 SYSTEM_ERROR, ILLEGAL_ARGUMENT 等其實就相當於 ErrorCodeEnum 的一個執行個體而已,因為它們是調用ErrorCodeEnum 的私人建構函式產生的。而 ErrorCodeEnum 的屬性 String code 和 String desc,是為了更好的提供更加詳細的錯誤資訊而定義的。而且在枚舉ErrorCodeEnum中還可以定義其它的各種輔助方法。

所以枚舉的本質是一個繼承與java.lang.Enum的類,枚舉常量就是枚舉的一個個的執行個體。枚舉可以有屬性和方法,來強化枚舉的功能。枚舉一般而言在Java中不是很好理解,一般掌握了枚舉背後的本質,那麼理解起來就毫無難度了。

3. 枚舉的常用方法

 

    public static void main(String[] args){        System.out.println(SYSTEM_ERROR.name());        System.out.println(SYSTEM_ERROR.ordinal());        System.out.println(SYSTEM_ERROR.toString());        for(ErrorCodeEnum e : ErrorCodeEnum.values()){            System.out.println(e.name());            System.out.println(e.getDesc());        }                System.out.println(ErrorCodeEnum.SYSTEM_ERROR.name());
System.out.println(ErrorCodeEnum.valueOf(ErrorCodeEnum.class, "ILLEGAL_ARGUMENT")); }

 

String name() : Returns the name of this enum constant, exactly as declared in its enum declaration. 返回枚舉常量聲明時的字串。

int    ordinal() : 返回枚舉常量的聲明時的順序位置,像數組的索引一樣,從0開始。

valueOf(Class<T> enumType, String name) : 其實是從 枚舉常量的字串到 枚舉常量的轉換,相當於一個Factory 方法。

name() 方法是從 枚舉常量 到 字串的轉換,而 valueOf 是字串到 枚舉常量的轉換。

values() : 該方法是一個隱式的方法,All the constants of an enum type can be obtained by calling the implicit public static T[] values() method of that type. 用於遍曆枚舉中的所有的枚舉常量。

4. enum相關的資料結構:EnumMap, EnumSet 具體可以參考jdk文檔。

5. enum 相對於 常量的優勢(略)

 

深入掌握Java中的enum

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.