Java提取常量的方式

記錄,備忘……

1 採用接口類

package test.constant;

/**
 *  常量類
 *      子內部類
 */
public interface Constant {

    /**
     *  公共常量
     */
    class Public{
        /** 用戶表 */
        public static final String TABLE = "test_user";
    }

    /**
     *  數字
     */
    class Number{
        /** 數字0 */
        public static final Integer ZERO = 0;
        /** 數字1 */
        public static final Integer ONE = 1;
    }

}

2 採用枚舉類

package test.constant;

/**
 *  枚舉類
 *      子枚舉類
 */
public class Enums {

    /**
     *  性別
     */
    public enum Sex{
        MALE("男"),
        FEMALE("女");

        private Sex(String value){
            this.value = value;
        }
        private final String value;

        public String getValue() {
            return value;
        }
    }

}

 

以上兩種方式比較推薦。

使用實例:

    public static void main(String[] args) throws Exception{
        System.out.println(Enums.Sex.FEMALE.getValue());
        System.out.println(Constant.Number.ONE);
        System.out.println(Constant.Public.TABLE);
    }

 

 

 

完.

 

 

 

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