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;
    }


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