Java双属性枚举使用

最近有小伙伴问我,双枚举类该怎么写,还得包括根据key取值方法。 于是就手写一个案例如下:

/**
 * 关系类型枚举
 * @author : shijing
 * 2018年4月25日下午5:58:54
 */
public enum RelationType {
	
	MAPPING(0,"映射"),
	QUOTE(1,"引用/授权"),
	ENTRUST(2,"委托"),
	AGENT(3,"代理"),;
	
	private int value;
	private String desc;
	
	RelationType(int value ,String desc) {
		this.value = value;
		this.desc = desc;
	}
	
	public int getValue() {  
        return value;  
    } 
	
	public String getDesc() {
		return desc;
	}

	public static String getDescByValue(int value) {
		for (RelationType enums : RelationType.values()) {
			if (enums.getValue() == value) {
				return enums.getDesc();
			}
		}
		return "";
	}

}

猜你喜欢

转载自blog.csdn.net/shijing266/article/details/80083360