枚举用法实例

清疚 2022-06-06 02:24 323阅读 0赞

枚举:常用的方法
通过实体类直接调用– 例子:
HTradeOnlineMerchantOrder.orderTypeEnum .getNameByCode(nCode);

  1. public enum orderTypeEnum {
  2. Online(1, "线上订单"), BelowTheLine(2, "线下订单") ;
  3. private int nCode;
  4. private String name;
  5. private orderTypeEnum(int _nCode, String _name) {
  6. this.nCode = _nCode;
  7. this.name = _name;
  8. }
  9. public int getCode() {
  10. return this.nCode;
  11. }
  12. public static String getNameByCode(int nCode) {
  13. for (HTradeOnlineMerchantOrder.orderTypeEnum c : HTradeOnlineMerchantOrder.orderTypeEnum.values()) {
  14. if (c.getCode() == nCode) {
  15. return c.name;
  16. }
  17. }
  18. return null;
  19. }
  20. }
  21. public enum statusEnum {
  22. //订单的状态 0=未支付 1= 支付中 2= 已支付 3=未发货 4= 已发货
  23. Unpaid("NO_PAY", "未支付"), Payment("Payment", "支付中"),AlreadyPaid("SUCC_REFUND", "已支付"), NotShipped("NotShipped", "未发货") , Shipped("Shipped", "已发货");
  24. private String nCode;
  25. private String name;
  26. private statusEnum(String _nCode, String _name) {
  27. this.nCode = _nCode;
  28. this.name = _name;
  29. }
  30. public String getCode() {
  31. return this.nCode;
  32. }
  33. public String getName() {
  34. return this.name;
  35. }
  36. public static String getNameByCode(String nCode) {
  37. for (HTradeOnlineMerchantOrder.statusEnum c : HTradeOnlineMerchantOrder.statusEnum.values()) {
  38. if (c.getCode().equals(nCode)) {
  39. return c.name;
  40. }
  41. }
  42. return null;
  43. }
  44. }

另外:想深入学习枚举的朋友可以看看这个帖子,里面包含了枚举使用的7种方法
http://www.cnblogs.com/felicityxi/p/6710231.html

后续:欢迎各位朋友一起探讨编程中的问题,请兴趣的朋友可以添加我的【微信公众号】

拿出手机扫一扫加关注

发表评论

表情:
评论列表 (有 0 条评论,323人围观)

还没有评论,来说两句吧...

相关阅读

    相关 类型enum用法赋值

    枚举常量是一种枚举类型中的值,及枚举值,枚举类型是由用户自定义的,只用用户在程序中定义它才能被使用。创建一个枚举类型的基本语法: enum 枚举类型名\{ 枚举值1,枚举值