一、枚舉類使用狀況一:java
1 package com.bie.util; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 /** 7 * 8 * @author biehl 9 * 10 * @date 2018年8月2日上午9:18:16 11 * 12 * @Notes 枚舉,返回登錄結果案例 13 * 14 */ 15 public enum LoginResult { 16 17 LOGIN_SUCCESS(0, "登錄成功"), 18 LOGIN_FAILED(1, "登錄失敗"), 19 LOGIN_ACCOUNT_NO(2, "登錄帳號不存在"), 20 LOGIN_ACCOUNT_ERROR(3, "登錄帳號錯誤"), 21 LOGIN_PASSWORD_ERROR(4, "登錄密碼錯誤"); 22 23 private int type;//類型 24 private String desc;//描述 25 //構造方法,決定了上面枚舉的字段 26 private LoginResult(int type, String desc) { 27 this.type = type; 28 this.desc = desc; 29 } 30 31 public int getType() { 32 return type; 33 } 34 public void setType(int type) { 35 this.type = type; 36 } 37 public String getDesc() { 38 return desc; 39 } 40 public void setDesc(String desc) { 41 this.desc = desc; 42 } 43 44 45 /** 46 * 根據type獲取到描述desc 47 * @param type 48 * @return 49 */ 50 public static String getResultDescByType(int type){ 51 //獲取到枚舉 52 LoginResult[] values = LoginResult.values(); 53 //增強for循環進行遍歷操做 54 for(LoginResult lr : values){ 55 //若是遍歷獲取的type和參數type一致 56 if(lr.getType() == type){ 57 //返回type對象的desc 58 return lr.getDesc(); 59 } 60 } 61 return null; 62 } 63 64 /** 65 * 根據type獲取到對應的enum 66 * @param type 67 * @return 68 */ 69 public static LoginResult getResultEnumByType(int type){ 70 //獲取到枚舉 71 LoginResult[] values = LoginResult.values(); 72 for(LoginResult lr : values){ 73 if(lr.getType() == type){ 74 return lr; 75 } 76 } 77 return null; 78 } 79 80 81 /** 82 * getChoiceMap 83 * @return 84 */ 85 public static Map<Integer, String> getChoiceMap(){ 86 Map<Integer, String> map = new HashMap<Integer, String>(); 87 for(LoginResult lr : LoginResult.values()){ 88 map.put(lr.getType(), lr.getDesc()); 89 } 90 return map; 91 } 92 93 public static void main(String[] args) { 94 //根據type獲取到對應的desc 95 //運行結果:登錄成功 96 //System.out.println(LoginResult.getResultDescByType(0)); 97 98 //能夠根據type獲取到對應的enum枚舉 99 //運行結果:LOGIN_SUCCESS 100 System.out.println(LoginResult.getResultEnumByType(0)); 101 102 //將type和desc封裝到map集合裏面 103 //運行效果:{0=登錄成功, 1=登錄失敗, 2=登錄帳號不存在, 3=登錄帳號錯誤, 4=登錄密碼錯誤} 104 //System.out.println(LoginResult.getChoiceMap()); 105 } 106 107 }
二、枚舉類使用狀況二:this
1 package com.bie.util; 2 3 /** 4 * 5 * @author biehl 6 * 7 * @date 2018年8月2日下午3:38:28 8 * 9 * @Notes REGISTER("註冊"),這種類型的枚舉能夠使用在調用此枚舉類而後使用switch來匹配到對應的方法 10 * 11 */ 12 public enum OperatorType { 13 14 REGISTER("註冊"), 15 LOGIN("登錄"), 16 INSERT("增長"), 17 DELETE("刪除"), 18 UPDATE("修改"), 19 SELECT("查詢"), 20 ; 21 22 //構造方法 23 private OperatorType(String desc){ 24 this.desc = desc; 25 } 26 27 private String desc;//描述 28 29 public String getDesc() { 30 return desc; 31 } 32 public void setDesc(String desc) { 33 this.desc = desc; 34 } 35 36 /** 37 * 根據desc獲取到enum 38 * @param desc 39 * @return 40 */ 41 public static OperatorType getResultEnumByDesc(String desc){ 42 OperatorType[] values = OperatorType.values(); 43 for(OperatorType ot : values){ 44 if(ot.getDesc() == desc){ 45 return ot; 46 } 47 } 48 return null; 49 } 50 51 52 public static void main(String[] args) { 53 //根據desc獲取到enum 54 //結果:DELETE 55 System.out.println(OperatorType.getResultEnumByDesc("刪除")); 56 57 } 58 59 }
三、枚舉類使用狀況三:spa
1 package com.bie.util; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 public enum GroupEnum { 7 8 GROUP_ONE(0,"group_one","一組"), 9 GROUP_TWO(1,"group_two","二組"), 10 GROUP_THREE(2,"group_three","三組"), 11 GROUP_FOUR(3,"group_four","四組"), 12 GROUP_FIVE(4,"group_five","五組"), 13 GROUP_SIX(5,"group_six","六組"), 14 GROUP_SENVEN(6,"group_senven","七組"), 15 ; 16 17 private GroupEnum(int id, String type, String desc) { 18 this.id = id; 19 this.type = type; 20 this.desc = desc; 21 } 22 23 private int id; 24 private String type; 25 private String desc; 26 27 public int getId() { 28 return id; 29 } 30 public void setId(int id) { 31 this.id = id; 32 } 33 public String getType() { 34 return type; 35 } 36 public void setType(String type) { 37 this.type = type; 38 } 39 public String getDesc() { 40 return desc; 41 } 42 public void setDesc(String desc) { 43 this.desc = desc; 44 } 45 46 /** 47 * 根據type獲取到對應的enum 48 * @param type 49 * @return 50 */ 51 public static GroupEnum getResultEnumByType(String type){ 52 GroupEnum[] values = GroupEnum.values(); 53 for(GroupEnum ge : values){ 54 if(ge.getType() == type){ 55 return ge; 56 } 57 } 58 return null; 59 } 60 61 /** 62 * 根據type獲取到對應的desc 63 * @param type 64 * @return 65 */ 66 public static String getResltDescByType(String type){ 67 GroupEnum[] values = GroupEnum.values(); 68 for(GroupEnum ge : values){ 69 if(ge.getType() == type){ 70 return ge.getDesc(); 71 } 72 } 73 return null; 74 } 75 76 /** 77 * 獲取到封裝的type和desc 78 * @return 79 */ 80 public static Map<String, String> getChoiceMap(){ 81 Map<String, String> map = new HashMap<String, String>(); 82 for(GroupEnum ge : GroupEnum.values()){ 83 map.put(ge.getType(), ge.getDesc()); 84 } 85 return map; 86 } 87 88 public static void main(String[] args) { 89 //根據參數2,type獲取到對應的enum 90 //運行結果:GROUP_ONE 91 //System.out.println(GroupEnum.getResultEnumByType("group_one")); 92 93 //根據type獲取到對應的desc 94 //運行結果:一組 95 //System.out.println(GroupEnum.getResltDescByType("group_one")); 96 97 //獲取到封裝好的type和desc 98 //運行結果:{group_senven=七組, group_six=六組, group_one=一組, group_five=五組, group_three=三組, group_two=二組, group_four=四組} 99 System.out.println(GroupEnum.getChoiceMap()); 100 } 101 102 }
待續.......code