java枚举

之前对枚举不是很了解,今天用到了,就明白了,直接放出一个枚举类型,说明一下用法:

 1 public enum FuluBillStatus {
 2 
 3     BILL_STATUS_unprocessed(1, "未处理"),
 4     BILL_STATUS_processed(2, "处理中"),
 5     BILL_STATUS_successful(3, "成功"),//(支付成功)
 6     BILL_STATUS_failed(4, "失败");//(支付失败)
 7 
 8     private final int index;
 9     private final String text;
10 
11     public int getIndex() {
12         return index;
13     }
14 
15     public String getText() {
16         return text;
17     }
18 
19     FuluBillStatus(int index, String text) {
20         this.index = index;
21         this.text = text;
22     }
23 
24     public static String getTextByIndex(int index) {
25         for (FuluBillStatus status : values()) {
26             if (status.index == index) {
27                 return status.text;
28             }
29         }
30         return "";
31     }
32 }
FuluBillStatus就相当于一个java类;
其中index和text就是这个类的2个属性;
FuluBillStatus(int index, String text) {}就是一个构造器;
BILL_STATUS_unprocessed(1, "未处理")就是创建了一个对象,示例中创建了4个对象,

因为属性都是用final修饰的,所以不能修改,一开始就创建了4个固定的对象,之后就不能再创建对象了;

getTextByIndex(int index)静态方法就是根据index得到对应的text【从4个对象中找对应的text】

备注说明:定义对象要放入属性定义之前,而且是用逗号, 分隔开多个对象,最后一个对象用分号;

使用枚举的场景:需要键值对的情况,考虑使用枚举

 


转载于:https://www.cnblogs.com/dwb91/p/10869808.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值