zoukankan      html  css  js  c++  java
  • Java枚举类使用和总结

    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 }

    待续.......

  • 相关阅读:
    C++编程之约定俗成的习惯(1)
    “亚信科技杯”南邮第七届大学生程序设计竞赛之网络预赛 (K L题解) CSS出题
    “亚信科技杯”南邮第七届大学生程序设计竞赛之网络预赛 (K L题解) CSS出题
    “亚信科技杯”南邮第七届大学生程序设计竞赛之网络预赛 (K L题解) CSS出题
    “亚信科技杯”南邮第七届大学生程序设计竞赛之网络预赛 (K L题解) CSS出题
    万万没想到(2) 南邮NOJ2058
    万万没想到(2) 南邮NOJ2058
    万万没想到(2) 南邮NOJ2058
    万万没想到(2) 南邮NOJ2058
    万万没想到(1) 南邮NOJ
  • 原文地址:https://www.cnblogs.com/biehongli/p/9408155.html
Copyright © 2011-2022 走看看