07-Enum

在开发中,使用到状态值(码表码值),不要直接代码里面写死对应的值,建议使用枚举来处理,利于修改

1. code

@Getter
public enum OoxxEnum {

    广东("", "GD"),
    广西("", "GX"),
    贵州("", "GZ"),
    ;

    private final String oldOoxxId;
    private final String newOoxxId;

    OoxxEnum(String oldOoxxId, String newOoxxId) {
        this.oldOoxxId = oldOoxxId;
        this.newOoxxId = newOoxxId;
    }

    public static OoxxEnum oldOoxxId(String oldOoxxId) {
        if (oldOoxxId == null) {
            return null;
        }
        for (OoxxEnum one : OoxxEnum.values()) {
            if (one.getOldOoxxId().equals(oldOoxxId)) {
                return one;
            }
        }
        return null;
    }

    public static OoxxEnum newOoxxId(String newOoxxId) {
        if (newOoxxId == null) {
            return null;
        }
        for (OoxxEnum one : OoxxEnum.values()) {
            if (one.getNewOoxxId().equals(newOoxxId)) {
                return one;
            }
        }
        return null;
    }

}

2. 获取name

java 枚举 示例_Java枚举name()方法及示例open in new window

OoxxEnum ooxxEnum = OoxxEnum.newOoxxId(currentOoxxId);
ooxxEnum.name()

3. 静态方法

@Getter
public enum OoxxEnum {

    common(0, "短信验证码:", ",请尽快填写完成验证,为保障您的账户安全,请勿外泄。"),

    login(1, "尊敬的用户,您本次登录验证码", ",请勿将验证码泄露给他人。如非本人操作,请忽略此短信。"),
    upd_mobile(2, "尊敬的用户,您正在修改手机号,本次验证码", ",请勿将验证码泄露给他人。如非本人操作,请忽略此短信。"),
    register(3, "尊敬的用户,您本次注册验证码", ",请勿将验证码泄露给他人。如非本人操作,请忽略此短信。"),
    ;

    private final int type;
    private final String prefix;
    private final String suffix;

    OoxxEnum(int type, String prefix, String suffix) {
        this.type = type;
        this.prefix = prefix;
        this.suffix = suffix;
    }

    public static OoxxEnum getByType(Integer type) {
        assert type != null;
        for (OoxxEnum one : OoxxEnum.values()) {
            if (one.getType() == type) {
                return one;
            }
        }
        return common;
    }
}




















 









OoxxEnum ooxxEnum = OoxxEnum.getByType(type);
- ooxxEnum.name()
- ooxxEnum.getPrefix()
- ooxxEnum.getSuffix()