枚舉類使用--持續更新

枚舉類的創建自己的構造函數,可以包含多個值。

public enum HelloStatus {
    dt_send(new Integer(-1),"hello"),
    none_send(new Integer(0),"world"),
    send_success(new Integer(1),"you");

    public Integercode;

    public String msg;
    // 構造方法
    LogisticsSendStatus(Integer c, String m){
        this.code = c;
        this.msg = m;
    }

    public static String getMsg(Integer s){
        if(s==null) return null;
        for (HelloStatus status : HelloStatus.values()) {
            if(status.code.compareTo(s)==0){
                return status.msg;
            }
        }
        return "";
    }

    public  String  geMsg(){
        return msg;
    }
}

使用:

@Test
public void test() {
        System.out.println(HelloStatus.dt_send.getMsg());
        System.out.println(HelloStatus.dt_send.code);
}

用枚舉來保存常量,個人覺得是一個很好的方式,大家公共管理,而且常量分類也很清楚,簡單易懂。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章