Java枚举使用笔记
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11833790.html
Java枚举简单使用示例:
package com.shineyue.sreport.operaterecord; /** * TODO 操作记录注解类枚举*/ public enum OperateRecordEnum { SELECT("SELECT", "查询"), DELETE("DELETE", "删除"), ADD("ADD", "增加"), UPDATE("UPDATE", "更新"); private String key; private String value; OperateRecordEnum(String key, String value) { this.key = key; this.value = value; } /** * 根据key获取value的值 * * @param key * @return */ public static String getValueByKey(String key) { for (OperateRecordEnum s : OperateRecordEnum.values()) { if (s.getKey() == key) { return s.getValue(); } } return null; } /** * 根据匹配value的值获取key * * @param channelName * @return */ public static String getKeyByValue(String channelName) { for (OperateRecordEnum s : OperateRecordEnum.values()) { if (channelName.equals(s.getValue())) { return s.getKey(); } } return null; } public String getKey() { return key; } public String getValue() { return value; } }