且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

枚举类的定义和使用

更新时间:2022-09-08 14:47:54

枚举是定义有限数量(或者说较少数量)的事物类别。

比如性别分“男”和“女”。中国的民族有56个等。这些都是能够一枚一枚列举出来的。


下面是一个枚举的定义和使用方法。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//定义枚举类。   
    public class CMSConstants{
        public enum CardStatus {
            库存("1"), 未发("2"), 激活("3"), 挂失("4"), 注销("5"), 换卡("6"), 补卡("7"), 待收货("8");
            private final String value;
            private final String text;
 
            /**
             
             */
            private CardStatus(String v) {
                this.value = v;
                this.text = this.name();
            }
 
            /**
             * @return the value
             */
            public String getValue() {
                return value;
            }
 
            /**
             * @return the text
             */
            public String getText() {
                return text;
            }
        }
    }      
        
1
2
3
4
        //使用枚举类。
        System.out.println(CMSConstants.CardStatus.激活.getValue());
        //CardQuery cardQuery=new CardQuery();
        //cardQuery.setStatus(CMSConstants.CardStatus.激活.getValue());//3



      本文转自建波李 51CTO博客,原文链接:http://blog.51cto.com/jianboli/1874610,如需转载请自行联系原作者