1. 程式人生 > >switch case 中如何使用列舉

switch case 中如何使用列舉

列舉類: 

public enum ContractTempletEnum {
	PPT(1,"普通模板"),SIGNT(2,"簽約模板");
	private Integer id;
	private String name;
	
	private ContractTempletEnum(Integer id, String name) {
		this.id = id;
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * 轉換list
	 * @return
	 */
	public static List<Map<String, Object>> toList(){
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		ContractTempletEnum[] contractTempletEnum = ContractTempletEnum.values();
		for(ContractTempletEnum e : contractTempletEnum) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", e.getId());
			map.put("name", e.getName());
			list.add(map);
		}
		return list;
	}
	

	public static ContractTempletEnum getById(Integer id){
        for(ContractTempletEnum transactType : values()){
        	if (transactType.getId() == id) {
                   //獲取指定的列舉
				return transactType;
			}
        }
        return null;
    }
	
}

測試類

public class TestEnum {
	
	public static void main(String[] args) {
		switch (ContractTempletEnum.getById(1)) {
		case PPT:
			
			break;

		case ESIGNT:
			break;
		}
	}
}