java常量定义方式

  Seves

    应该都会遇到在项目中定义一些常量,通常有三种实现方式:
    a. 接口常量
    b. 类常量
    c. 枚举
    我们一起了解下这几种

    1. 接口常量
    public interface SwingConstants {
        /**
         * The central position in an area. Used for
         * both compass-direction constants (NORTH, etc.)
         * and box-orientation constants (TOP, etc.).
         */
        int CENTER  = 0;
    
    
        /**
         * Box-orientation constant used to specify the top of a box.
         */
        int TOP     = 1;
        /**
         * Box-orientation constant used to specify the left side of a box.
         */
        int LEFT    = 2;
    
    }
    

    或者

    public interface UtilConstants {
        /**
         * 公共常量
         */
        class Public {
            public static final String ID = "TESTID";
        }
    
        /**
         * JSP路径
         */
        class JspFilePath {
            public static final String TESTCONTROLLER = "jsp/basic/";
            public static final String TEMPLATE_PAGEPATH = "basic/template/"; // 模板(测试)
        }
    
        /**
         * vo 对象的一些公共的属性名称
         */
        class VoFields {
            public static final String ACTIONTIME = "operateTime";//操作时间
            public static final String ACTIONUSERNAME = "operatorName";//操作人姓名
            public static final String CHECKTIME = "auditTime";//审核时间
            public static final String CHECKUSERID = "checkUserId";//审核人ID
            public static final String CHECKUSERNAME = "auditPerson";//审核人姓名
            public static final String CREATETIME = "createTime";        // 创建时间
            public static final String CREATEUSERID = "createUserId";// 创建人code
            public static final String INSERTUSERNAME = "createUserName";// 创建人姓名
            public static final String UPDATETIME = "updateTime";        // 修改时间
            public static final String UPDATEUSERID = "updateUserId";// 修改人CODE
            public static final String UPDATEUSERNAME = "updateUserName";// 修改人姓名
            public static final String DELFLAG = "delFlag";             // 删除标记
            public static final String DBID = "dbid";                    // 主键
        }
    }
    

    优点:
    - 简洁,不用写public static final

    缺点:

    • 需要看注释才能知道常量对应的信息
    • 《Effective Java》这本书中提到过:如果某个实现了常量接口的类被修改不再需要常量了,也会因为序列化兼容原因不得不保持该实现,而且非final类实现常量接口会导致所有子类被污染
    1. 类常量
    public class ResponseCode {
        /** 系统处理正常 */
        public static final int SUCCESS_HEAD = 0;
    
        /** 系统处理未知异常 */
        public static final int EXCEPTION_HEAD = 1;
    
        /** JSON解析错误 */
        public static final int JSON_RESOLVE = 2;
    
        /** 类型不匹配 */
        public static final int TRANSTYPE_NO = 3;
    
        /** Head - messageID未赋值 */
        public static final int HEAD_messageID = 4;
    
        /** Head - timeStamp未赋值 */
        public static final int HEAD_timeStamp = 5;
    
        /** Head - messengerID未赋值 */
        public static final int HEAD_messengerID = 6;
    
        /** Head - transactionType 未赋值 */
        public static final int HEAD_transactionType = 7;
    
        /** digest校验不通过 */
        public static final int HEAD_DIGEST = 8;
    
        /** src校验不通过 */
        public static final int HEAD_SRC_NULL = 10;
    
        /** 协议包含非法字符 */
        public static final int ILLEGAL_MESSAGE = 11;
    
        /** 数据库异常 */
        public static final int DATABASE_EXCEPTION = 9;
        public static final Map<Integer, String> RESP_INFO = new HashMap<Integer, String>();
    
    1. 枚举
    public enum Size {
        SMALL("S"),
        MEDIUM("M"),
        LARGE("L"),
        EXTRA_LARGE("XL");
    
        /**
         * 缩写
         */
        private String suoxie;
    
        private Size(String suoxie){
            this.suoxie = suoxie;
        }
    
        public String getSuoxie(){
            return suoxie;
        }
    }
    
    1. 个人最推荐的方式: 类内枚举
    public class Constants {
        public enum Dict{
            PROSTA("PROSTA","产品状态"),
            COUNTRY("COUNTRY","国家"),
    
            YWLX("YWLX","业务类型"),
            INDUSTRYCOMPANYTYPE("IndustryCompanyType","公司类型"),
            JSFS("JSFS","结算方式"),
            COMMISSIONTYPE("COMMISSIONTYPE","返佣类型"),
            BALUNITTYPE("BALUNITTYPE","结算单位类型"),
            ORDERSTATS("OrderStats","订单状态"),
            BACKORDERSTATUS("BackOrderStatus","退单审核状态"),
            BUSINESSPAYMENT("BusinessPayment","业务款项"),
            ENABLESTATE("enableState","启用禁用"),
            APPROVESTATE("approveState","审批状态"),
            //        分销系统所需是商品系统的xml
            PRODUCTCONTENTTYPE("productContentType","商品内容分类"),
            IDENTITY("identity","适应人群"),
            AREA("area","领区"),
            VISATYPE("visatype","签证类型"),
            SERVICETYPE("serviceType","公证认证商品内容分类"),
            PRODUCTTYPEQUALITY("productTypeQuality","公证认证商品性质"),
            EXPRESSTYPE("expresstype","公证认证加急种类"),
            IDETIFICATIONTYPE("identificationType","认证类别"),
            QYKHLX("QYKHLX","客户类型"),
            ZILIAONAME("ziliaoName","资料名称"),
            YESORNO("yesOrNo","是否");
    
    
    
            Dict(String value, String name){
                this.value=value;
                this.name=name;
            }
            private final String value;
            private final String name;
    
            public String getValue() {
                return value;
            }
    
            public String getName() {
                return name;
            }
        }
        /**
         * 订单状态
         * <p>Company:rayootech</p>
         * @author zhangxueshen
         * @date 2016-6-14
         */
        public enum OrderStats{
    
            DELETE(0,"删除"),RESERVE(1,"订单预定"),CONFIRM(2,"订单确认"),COMPLETE(3,"订单完成"),CLOSE(4,"订单关闭");
    
            OrderStats(Integer value, String name){
                this.value = value;
                this.name = name;
            }
            private final Integer value;
            private final String name;
    
            public Integer getValue() {
                return value;
            }
    
            public String getName() {
                return name;
            }
    
        }
    }
    
    

    作者 @没有故事的老大爷
    我们把在黑暗中跳舞的心脏叫做月亮,这月亮主要由你构成。

    33