java枚举类型实践

公告类型的枚举类型

public enum BulletinsType {
    DELAY_BULLETINS("延期公告",7),
    FLOWMARK_BULLETINS("流标公告",8),
    RUNOFF_BULLETINS("废标公告",10),
    PREQUALIFICATION_BULLETINS("资格预审公告",2),
    CLEAR_BULLETINS("澄清公告",3),
    FRUIT_BULLETINS("中标结果公告",4),
    CHANGE_BULLETINS("变更公告",5),
    BEFOREHAND_BULLETINS("预中标公告",6),
    EVALUATION_BULLETINS("评标报告",9);
    private String typeName;  
    private int index;
    BulletinsType(String typeName , int index) {  
        this.typeName = typeName;  
        this.index = index;  
    }  
    /** 
     * 根据公告类型的索引,返回类型的枚举实例。 
     * @param typeName 类型名称 
     */  
    public static BulletinsType fromIndex(int index) {  
        for (BulletinsType type : BulletinsType.values()) {  
            if (type.getIndex() == index) {  
                return type;  
            }  
        }  
        return null;  
    }     
    /** 
     * 根据类型的名称,返回类型的枚举实例。 
     * @param typeName 类型名称 
     */  
    public static BulletinsType fromTypeName(String typeName) {  
        for (BulletinsType type : BulletinsType.values()) {  
            if (type.getTypeName().equals(typeName)) {  
                return type;  
            }  
        }  
        return null;  
    }  
    public String getTypeName() {  
        return this.typeName;  
    }      
    public int getIndex() {  
        return this.index;  
    }  
//这段代码,通过key就能获得对应的枚举类型
BulletinsType  bulletinsType = BulletinsType.fromIndex(bulletins.getCodeType());

 公告类型是固定的,业务系统通过codeType就能直接从枚举类中获得类型名字了.

设计原则:重复的字符串应该使用 静态变量或者枚举类,一处更新,多出使用

原文地址:https://www.cnblogs.com/jkwll/p/11375262.html