枚举类型使用的最佳实践

来源:互联网 发布:java中setscale 编辑:程序博客网 时间:2024/06/10 21:53
import org.apache.commons.lang.StringUtils;public enum CertTypeEnum {    身份证(0, "身份证"),    护照(1, "护照"),    军官证(2, "军官证"),    士兵证(3, "士兵证"),    回乡证(4, "回乡证"),    临时身份证(5, "临时身份证"),    户口簿(6, "户口簿"),    警官证(7, "警官证"),    台胞证(8, "台胞证"),    营业执照(9, "营业执照"),    其它证件(10, "其它证件"), ;    /**     * Getter method for property <tt>code</tt>.     *      * @return property value of code     */    public int getCode() {        return code;    }    /**     * Setter method for property <tt>code</tt>.     *      * @param code value to be assigned to property code     */    public void setCode(int code) {        this.code = code;    }    /**     * Getter method for property <tt>desc</tt>.     *      * @return property value of desc     */    public String getDesc() {        return desc;    }    /**     * Setter method for property <tt>desc</tt>.     *      * @param desc value to be assigned to property desc     */    public void setDesc(String desc) {        this.desc = desc;    }    private CertTypeEnum(int code, String desc) {        this.code = code;        this.desc = desc;    }    /** code */    private int    code;    /** desc */    private String desc;    public static CertTypeEnum getByCode(String code) {        if (StringUtils.isBlank(code)) {            return null;        }        for (CertTypeEnum item : values()) {     //values()是枚举的静态方法            if (StringUtils.equals(String.valueOf(item.getCode()), code)) {                return item;            }        }        return null;    }}

0 0
原创粉丝点击