實際項目中有用常量的也有用枚舉的,那么他們有什么區別和聯系呢?
沒區別、沒聯系、優先使用枚舉
原因:枚舉更加靈活,使用性多樣
枚舉:
public enum RespEnum { SUCCESS("0000","成功"), ERROR_SYSERR("0010","失敗"); public String respCd; public String respMsg; RespEnum(String respCd, String respMsg){ this.respCd =respCd; this.respMsg = respMsg; } }
常量:
public class RespConstants { public static final String RESPCD_SUCCESS="0000"; public static final String RESPCD_ERROR="0010"; @SuppressWarnings("serial") public static final Map<String, String> RESPMSG = new LinkedHashMap<String, String>() { { put(RESPCD_SUCCESS, "成功");
put(RESPCD_ERROR, "失敗");
}
};
}