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 }

待续.......

Java枚举类使用和总结相关推荐

  1. Java枚举类使用方式

    Java枚举类使用方式 单枚举定义 : /*** * @title: 单枚举定义* @author: wll* @since: 2021-1-27 14:47:54*/ public enum Jud ...

  2. 比起睡觉,我更喜欢刷巨详细的Java枚举类,这是来自猿人的自觉呀

    零基础学习之Java枚举类 概述 JDK1.5之前 创建枚举类 代码示例 JDK1.5之后 创建枚举类 代码示例 枚举类继承父类 基本介绍 代码示例 枚举类实现接口 基本介绍 代码示例 枚举类实现单例 ...

  3. 学妹问我Java枚举类与注解,我直接用这个搞定她!

    很多人问我学妹长什么样,不多说 上图吧! 学妹问我Java枚举类与注解,我直接一篇文章搞定! 一.枚举类 ① 自定义枚举类 ② enum关键字定义枚举类 ③ enum 枚举类的方法 ④ enum 枚举 ...

  4. java枚举类中字段有没有必要加final____枚举类字段 Field ‘xxx‘ may be ‘final‘

    java枚举类中字段有没有必要加final 今天在写一个系统统一返回码的枚举类时候,突然想到一个问题,当不小心手抖给枚举类自动生成了set方法,而恰巧在用的地方不小心用了set方法,从而修改了code ...

  5. JAVA 枚举类的初步理解

    JAVA 枚举类的初步理解 现在Java的枚举类在真实项目中已经用的比较频繁,比静态常量更好用,也更有限定性,enum类可以用来表示有限的类对象,比如星期.月份.性别或者项目中的产品类型 像诸如此类的 ...

  6. java 枚举类组合在一起_Java,.NET,但为什么在一起?

    java 枚举类组合在一起 十二年前,Sun微系统公司大张旗鼓地宣布了一种新的编程语言和环境,用于使网页更具动态性和"活力". 当然,现在,Java编程语言是一种无处不在的工具,它 ...

  7. java 枚举类型enum ppt,关于JAVA枚举类使用的异常

    当前位置:我的异常网» J2SE » 关于JAVA枚举类使用的异常 关于JAVA枚举类使用的异常 www.myexceptions.net  网友分享于:2013-01-24  浏览:5次 关于JAV ...

  8. Java枚举类使用要点

    Java枚举类 Java中的枚举类型定义使用enum关键字,定义时,系统默认继承Enum抽象类.先来看一下枚举类的使用. enum Sex {// 枚举对象必须要在第一行MAIL("男&qu ...

  9. Java枚举类的写法

    Java枚举类的写法 枚举也是一个类(枚举对象,属性,构造器,get方法),由于是枚举,一般我们只需要得到值,而不需要set值

最新文章

  1. js控制页面元素的隐藏与显示
  2. 机器人学习--粒子滤波SLAM/MCL定位参考资料+学习经验
  3. html怎么建立段落,HTML 段落
  4. shared_ptrT make_shared( Args ... args );
  5. 修改折半查找算法进行范围查找
  6. 汇编语言之第六章包含多个段的程序
  7. java m2文件放哪里_windows下打开.m2文件夹,没有找到setting.xml
  8. 萌新的Python练习菜鸟100例(十八)求s=a+aa+aaa+aaaa+aa...a的值
  9. 浅谈@RequestParam与@RequestBody区别
  10. ubuntu 12.04 3D特效
  11. 23. 客户默认选项(Default Customer Options)
  12. Win系统 - WIN10 版本号说明
  13. 山东大学项目实训(二十七)—— 微信小程序开发总结,一年时间真的可以改变一个人很多
  14. 网吧网管新人对无盘技术不熟
  15. Win11使用PCVR时性能问题已修复 可手动安装解决
  16. 低代码平台和专业开发人员——完美搭档?
  17. Vuforia Ground Plane 平面识别
  18. Bootstrap可以这样学-曹领雄-专题视频课程
  19. 2020-10-14
  20. 截图神器-Ashampoo Snap 10.0.8 中文免费版

热门文章

  1. 人脸识别争议不断 中美市场冰火两重天
  2. Waymo无人卡车高调重返凤凰城,但货运先机已失
  3. Facebook的首席技术官:人工智能已用于内容审核,未来会做更多
  4. 风之语.我看苏州511房产新政
  5. 深度丨霍金的去世让我想起了微软提出的人工智能十大原则
  6. (完全解决)Key already registered with the same priority: GroupSpatialSoftmax
  7. 欧阳自远:有个性的嫦娥12345,如何不重复美国探月路?
  8. 吴恩达:2020 年,这些 AI 大事件让我无法忘怀...
  9. 重磅盘点:过去8年中深度学习最重要的想法
  10. 人工智能70年:科幻和现实的交融