前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java枚举类使用和总结

Java枚举类使用和总结

作者头像
别先生
发布2018-08-09 16:12:02
5800
发布2018-08-09 16:12:02
举报
文章被收录于专栏:别先生别先生别先生

1、枚举类使用情况一:

  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 }

2、枚举类使用情况二:

 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 }

3、枚举类使用情况三:

  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 }

待续.......

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-08-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档