java 枚举类非常好的运用实例

2021/8/25 11:35:59

本文主要是介绍java 枚举类非常好的运用实例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

枚举类 举例:

 1 import lombok.Getter;
 2 
 3 public class PublicConstants {
 4     public enum StatusEnum {
 5 
 6         STATUS_TEMP_STORAGE(1,"暂存", "待提交"),
 7         STATUS_AUDI_TING(2,"审批中"),
 8         STATUS_AUDIT_PASS(3,"审批通过","审批通过","**确认·待***确认"),
 9         STATUS_AUDIT_REJECT(4,"审批驳回"),
10         STATUS_APPEAL(5,"申诉中", "申诉"),
11         STATUS_CANCEL(6,"已作废"),
12         STATUS_FINISH(7,"完成","完成","***已确认"),
13         STATUS_DELETE(8,"删除"),
14         STATUS_REVOKE(9,"已撤销");
15 
16         @Getter/** 码值 **/
17         private Integer code;
18         @Getter/** 名称 **/
19         private String name;
20         @Getter/** 别名 **/
21         private String alias;
22         @Getter/** 描述 **/
23         private String desc;
24 
25         StatusEnum(Integer code, String name, String alias, String desc) {
26             this.code = code;
27             this.name = name;
28             this.alias = alias;
29             this.desc = desc;
30         }
31 
32         StatusEnum(Integer code, String name, String alias) {
33             this.code = code;
34             this.name = name;
35             this.alias = alias;
36             this.desc = name;
37         }
38 
39         StatusEnum(Integer code, String name) {
40             this.code = code;
41             this.name = name;
42             this.alias = name;
43             this.desc = name;
44         }
45 
46         //通过描述获取code
47         public static int getCode(String name){
48             StatusEnum[] values = StatusEnum.values();
49             for(int i=0;i<values.length;i++){
50                 StatusEnum be = values[i];
51                 if(be.name.equalsIgnoreCase(name)){
52                     return be.code;
53                 }
54             }
55             return 0;
56         }
57 
58         //通过code获取枚举对象
59         public static StatusEnum getEnumByCode(Integer code) {
60             for (StatusEnum flagEnum : StatusEnum.values()) {
61                 if (code.equals(flagEnum.getCode())) {
62                     return flagEnum;
63                 }
64             }
65             return null;
66         }
67     }
68 
69 }

 

运用:

en.setStateName(PublicConstants.StatusEnum.getEnumByCode(en.getStatus()).getName());

说明:使用 常量类 PublicConstants 是为了 代码提示,方便编程。



这篇关于java 枚举类非常好的运用实例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程