Java常量的实现
1、接口常量:接口里的变量默认为static final的特性,
(1)接口常量如java的swing里有一个SwingConstant
public interface SwingConstants{
/**
* The central position in an area. Used for
* both compass-direction constants (NORTH, etc.)
* and box-orientation constants (TOP, etc.).
*/
public static final int CENTER =0;
//
// Box-orientation constant used to specify locations in a box.
//
/**
* Box-orientation constant used to specify the top of a box.
*/
public static final int TOP =1;
....
}
(2)接口里面定义静态内部类,可以把不同功能的常量类进一步分类,如下:
public interface UtilConstants{
/**
*公共常量
*/
public static class Public{
public static final String ID="TESTID";
}
/**
*JSP路径
*/
public static class JspFilePath{
public static final String TESTCONTROLLER ="jsp/basic/";
}
}
2、常量类:定义了类常量,用一个Map<Integer, String>来封装常量对应的信息,在static代码块里,类初始化的时候执行一次put。用的时候ResponseCode.RESP_INFO.get("DATABASE_EXCEPTION");就能取出响应信息
public class ResponseCode{
/**系统处理正常*/
public static final int SUCCESS_HEAD=0;
/**系统处理未知异常*/
public static final int EXCEPTION_HEAD=1;
public static final Map<Integer ,String> RESP_INFO = new HashMap<Integer,String>();
static{
RESP_INFO.put(SUCCESS_HEAD,"系统处理正常");
RESP_INFO.put(EXCEPTION_HEAD,"系统处理未知异常");
}
}
3、枚举
(1)普通枚举
public enum Season{
SPRING,SUMMER,AUTUMN,WINTER
}
(2)类中定义枚举:由于系统可能用到的枚举很多,如果每个枚举都单独创建一个枚举类的话就太多,故可以在常量类中定义多个枚举,由于接口中无法定义静态代码库,故选择常量类:
public class Constants{
public static enum Dict{
YWLX("YWLX","业务类型"),
JSFS("JSFS","结算方式");
private String value;
private String name;
private Dict(String value,String name){
this.value= value;
this.name=name;
}
...
}
/**
*订单状态
*/
public static enum OrderStats{
DELETE(0,"删除"),RESERVE(1,"订单预定");
private OrderStats(Integer value,String name){
this.value = value;
this.name = value;
}
...
}
}
1、接口常量:接口里的变量默认为static final的特性,
(1)接口常量如java的swing里有一个SwingConstant
public interface SwingConstants{
/**
* The central position in an area. Used for
* both compass-direction constants (NORTH, etc.)
* and box-orientation constants (TOP, etc.).
*/
public static final int CENTER =0;
//
// Box-orientation constant used to specify locations in a box.
//
/**
* Box-orientation constant used to specify the top of a box.
*/
public static final int TOP =1;
....
}
(2)接口里面定义静态内部类,可以把不同功能的常量类进一步分类,如下:
public interface UtilConstants{
/**
*公共常量
*/
public static class Public{
public static final String ID="TESTID";
}
/**
*JSP路径
*/
public static class JspFilePath{
public static final String TESTCONTROLLER ="jsp/basic/";
}
}
2、常量类:定义了类常量,用一个Map<Integer, String>来封装常量对应的信息,在static代码块里,类初始化的时候执行一次put。用的时候ResponseCode.RESP_INFO.get("DATABASE_EXCEPTION");就能取出响应信息
public class ResponseCode{
/**系统处理正常*/
public static final int SUCCESS_HEAD=0;
/**系统处理未知异常*/
public static final int EXCEPTION_HEAD=1;
public static final Map<Integer ,String> RESP_INFO = new HashMap<Integer,String>();
static{
RESP_INFO.put(SUCCESS_HEAD,"系统处理正常");
RESP_INFO.put(EXCEPTION_HEAD,"系统处理未知异常");
}
}
3、枚举
(1)普通枚举
public enum Season{
SPRING,SUMMER,AUTUMN,WINTER
}
(2)类中定义枚举:由于系统可能用到的枚举很多,如果每个枚举都单独创建一个枚举类的话就太多,故可以在常量类中定义多个枚举,由于接口中无法定义静态代码库,故选择常量类:
public class Constants{
public static enum Dict{
YWLX("YWLX","业务类型"),
JSFS("JSFS","结算方式");
private String value;
private String name;
private Dict(String value,String name){
this.value= value;
this.name=name;
}
...
}
/**
*订单状态
*/
public static enum OrderStats{
DELETE(0,"删除"),RESERVE(1,"订单预定");
private OrderStats(Integer value,String name){
this.value = value;
this.name = value;
}
...
}
}
参考:https://www.cnblogs.com/lihaoyang/p/6913295.html