枚举常用方式

枚举常用方式

ordinal 枚举下标从0开始

int ordinal = SUCCESS.ordinal();  获取枚举下标
BusinessStatus success = BusinessStatus.values()[SUCCESS.ordinal()];  通过下标获取枚举

name 枚举名称

String name = SUCCESS.name();  获取枚举名称
BusinessStatus success = BusinessStatus.valueOf("SUCCESS"); 通名称获取枚举

常规枚举

public enum BusinessStatus {
   /**
    * 成功
    */
   SUCCESS,

   /**
    * 失败
    */
   FAIL,
}

添加属性的枚举

public enum UserStatus {
   OK("0", "正常"), DISABLE("1", "停用"), DELETED("2", "删除");

   private final String code;
   private final String info;

   UserStatus(String code, String info) {
      this.code = code;
      this.info = info;
   }

   public String getCode() {
      return code;
   }

   public String getInfo() {
      return info;
   }
}

你可能感兴趣的:(枚举常用方式)