枚举用法实例
枚举:常用的方法
通过实体类直接调用– 例子:
HTradeOnlineMerchantOrder.orderTypeEnum .getNameByCode(nCode);
public enum orderTypeEnum {
Online(1, "线上订单"), BelowTheLine(2, "线下订单") ;
private int nCode;
private String name;
private orderTypeEnum(int _nCode, String _name) {
this.nCode = _nCode;
this.name = _name;
}
public int getCode() {
return this.nCode;
}
public static String getNameByCode(int nCode) {
for (HTradeOnlineMerchantOrder.orderTypeEnum c : HTradeOnlineMerchantOrder.orderTypeEnum.values()) {
if (c.getCode() == nCode) {
return c.name;
}
}
return null;
}
}
public enum statusEnum {
//订单的状态 0=未支付 1= 支付中 2= 已支付 3=未发货 4= 已发货
Unpaid("NO_PAY", "未支付"), Payment("Payment", "支付中"),AlreadyPaid("SUCC_REFUND", "已支付"), NotShipped("NotShipped", "未发货") , Shipped("Shipped", "已发货");
private String nCode;
private String name;
private statusEnum(String _nCode, String _name) {
this.nCode = _nCode;
this.name = _name;
}
public String getCode() {
return this.nCode;
}
public String getName() {
return this.name;
}
public static String getNameByCode(String nCode) {
for (HTradeOnlineMerchantOrder.statusEnum c : HTradeOnlineMerchantOrder.statusEnum.values()) {
if (c.getCode().equals(nCode)) {
return c.name;
}
}
return null;
}
}
另外:想深入学习枚举的朋友可以看看这个帖子,里面包含了枚举使用的7种方法
http://www.cnblogs.com/felicityxi/p/6710231.html
后续:欢迎各位朋友一起探讨编程中的问题,请兴趣的朋友可以添加我的【微信公众号】
还没有评论,来说两句吧...