常量和枚举的区别

实际项目中有用常量的也有用枚举的,那么他们有什么区别和联系呢?

 

没区别、没联系、优先使用枚举

 

原因:枚举更加灵活,使用性多样

 

枚举:

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, "失败"); 
   }
  };
}

 

posted @ 2020-12-08 14:32  时间会有答案  阅读(1417)  评论(0)    收藏  举报