enum 枚举 常用方法

   public static enum PayType{
        alipay_web,alipay_app,wechat,offline;
        
        public static  boolean isValid(String typeStr) {
            try {
                PayType type = Constants.PayType.valueOf(typeStr);
                if(type==null) {
                    return false;
                }
                return type instanceof PayType;
            } catch (Exception e) {
                return false;
            }
        }
    }
 
 
1 PayType.alipay_web.name()  = PayType.alipay_web.toString()
2 PayType.valueOf(str)  将str 转为 枚举对象
3 判断 字符串 是否是枚举中某一个常量的 name
见 isValid() 函数,使用: 
boolean valid = Constants.PayType. isValid(" alipay_web");  // true
boolean valid = Constants.PayType. isValid(" alipay_web1");  //false
 
 
 
 

你可能感兴趣的:(enum)