解决方案:枚举类根据key值获取value值。

时间:2025-02-11 07:13:48

下面是一个根据key值获取枚举类相应的value值的方法。

第一种方法

	public static String getValue(String code) {
		for (TestEnum ele : values()) {
			if(().equals(code)) return ();
		}
		return null;
	}

第二种方法

枚举类
public enum Test {
	A("Hello",0),B("World",1);
	private String name;
	private int code;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		 = name;
	}
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		 = code;
	}
	private Test(String name, int code) {
		 = name;
		 = code;
	}
}

测试类
public static void main(String[] args) {
	((,"A").getCode());
}

注意:建议优先使用第一种。

相关文章