遍历枚举类

public enum StatusType {
	//定义枚举值
	UNKNOWN(0, "未知"),
	NORMAL(1, "正常"),
	OFFLINE(2, "离线"),
	POWER_FAILURE(38, "电源断电");
	
	//定义属性
	private int code;
	private String name;
	
	//构造方法
	private StatusType(int code, String name) {
		//为属性赋值
		this.code = code;
		this.name = name;
	}

	//get和set方法
	public int getCode() {
		return code;
	}

	public String getName() {
		return name;
	}
	
	public static String getValueBykey(int code) {
		try {
			for(StatusType type:StatusType.values()) {
				if(type.getCode()==code) {
					return type.getName();
				}
			}
		}catch(Exception e) {
			return "未知";
		}
		return "未知";
	}
}

 

你可能感兴趣的:(java基础)