Java中枚舉及其構造函數

一、最簡單的枚舉

public enum Season{ Spring,Summer,Fall,Winter};

二、帶構造器的枚舉

如下:EOrderType就是枚舉的構造函數

例如NormalOrder(0, "一般訂單") 第一個0對於構造函數的type,第二個參數對應構造函數的desc

getOrderType

注意:1、需要在枚舉實例後面加上分號,然後再寫構造函數等

            2、枚舉實例必須在前面

            3、定義枚舉的構造器方法帶參,只能爲private 

public enum EOrderTyper {
	  /**
     * 無效的訂單類型
     */
	Invalid(-1,"無效的訂單類型"),
	
	/**
	 * 一般訂單
	 */
	NormalOrder(0, "一般訂單"),
	
	/**
	 * 虛擬禮品卡訂單
	 */
	VDDmoney(50, "虛擬訂單"),
	
	/**
	 * 實物禮品卡訂單
	 */
	PDDmoney(51, "禮品卡訂單");
	private int orderType;
	private String description;
	private EOrderTyper(int type, String desc) {
		this.orderType = type;
		this.description = desc;
	}


	public int getOrderType() {
		return this.orderType;
	}


	public String getDescription() {
		return this.description;
	}
	
}

三、EnumSET

可以用來創建枚舉集合,例如:

public static EnumSet<EPriceType> getSetTypes(){
      EnumSet<EPriceType> set=EnumSet.noneOf(EPriceType.class);  
      set.addAll(EnumSet.complementOf(set));  
      return set;
    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章