custom property class java enum

enum class custom properties

This is easy to use than static enum static variables place, you can give each enumeration value several properties, such as

Example 1:

public enum GasStationChannel { 
    EN ( "the technology", "100001"), 
    the APP ( "the APP", "100002"), 
    , QZ ( "skid", "100003"), 
    ZYW ( "find oil network", "100004" ), 
    YZG ( "oil dispensers", "100005, China"), 
    YZX ( "oil front", "100006"), 
    SHELL ( "Shell", "100007"), 
    CHEBEI ( "car chanting", "100008"), 
    SHANGAO ( "SDE", "100009"), 
    GUANDE ( "Kantons", "100010"); 

    Private String name; 
    Private String code; 

    GasStationChannel (String name, String code) { 
        this.name = name; 
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public static GasStationChannel parse(String code) {
        if(code==null){
            return null;
        }
        for (GasStationChannel channelType : GasStationChannel.values()) {
            if (channelType.getCode().equals(code)) {
                return channelType;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return "name:"+this.name+",code:"+this.code;
    }}

 Example 2:

public enum Domain {


    XB("11","西北"),
    HD("13","华东"),
    DB("14","东北"),
    HB("15","华北");

    private String code;
    private String name;

    Domain(String code,String name) {
        this.code = code;
        this.name = name;
    }

    public String getCode() {
        return code;
    }
    public String getName(){
        return name;
    }

    /**
     * 根据domain code,返回枚举类型
     */
    public static Domain getDomain(String code) throws Exception {

        Domain domain = null;
        switch (code.trim()) {
            case "11":
                domain = XB;
                break;
            case "13":
                domain = HD;
                break;
            case "14":
                domain = DB;
                break;
            case "15":
                domain = HB;
                break;
            default:
                throw new Exception(String.format("传入的域ID[%s]不存在,请检查!", code));
        }
        return domain;
    }
}

  Two or more can be achieved, according to personal preferences, individuals are more inclined to Example 1, the code structure is more beautiful

Guess you like

Origin www.cnblogs.com/unknows/p/11200246.html
Recommended