隨筆-8  評論-39  文章-0  trackbacks-0

          編寫程序有時會用到有序常量。一個常見的情況是對命令行參數進行處理,如下的代碼片段:

           

          public class A {

                     

                      …….

                      private static final int PARAM_1 = 0;

                      private static final int PARAM_2 = 1;

                      private static final int PARAM_3 = 2;

                      private static final int PARAM_4 = 3;

                      private static final int PARAMS_COUNT = 4;

                      …….

                     

                      public static void main(String[] args) {

                                 

                                  if (args.length != PARAMS_COUNT) {

                                              System.out.println("Usage: program <Param_1> <Param_2> <Param_3> <Param_3> <Param_4>");

                                              System.exit(1);

                                  }

                                  callFun1(args[PARAM_1], args[PARAM_2]);

                                  callFun2(args[PARAM_1], args[PARAM_3], args[PARAM_4]);

                      }

          }

           

          PARAM_1PARAM_4就是我這里說的有序產量,這樣的常量通常被賦予一段順序的值。但如果像上面這段代碼的寫法,將會在程序維護時帶來一些小麻煩。比如增加一個新的產量PARAM_5,除了需要定義型的常量以外,還需要調整PARAMS_COUNT的值。如果需要在原序列中插入一個新的常量,那就更麻煩了,不僅需要調整PARAMS_COUNT的值,還需要調整插入位置之后的常量的值。如在上面的代碼中插入一個常量PARAM_NEWPARAM_3的位置:

                     

                      …….

                      private static final int PARAM_1 = 0;

                      private static final int PARAM_2 = 1;

                      private static final int PARAM_NEW = 2          // New Constant

                      private static final int PARAM_3 = 3;                // Need Changed

                      private static final int PARAM_4 = 4;                // Need Changed

                      private static final int PARAMS_COUNT = 5;  // Need Changed

                      …….

                     

           

          其實我們只需要引入一個靜態變量就可以比較好的解決這個問題。下面是修改后的代碼:

           

          public class A {

                     

                      …….

                      private static int PARAM_INDEX = 0;

                      private static final int PARAM_1 = PARAM_INDEX++;

                      private static final int PARAM_2 = PARAM_INDEX++;

                      private static final int PARAM_3 = PARAM_INDEX++;

                      private static final int PARAM_4 = PARAM_INDEX++;

                      private static final int PARAMS_COUNT = PARAM_INDEX;

                      …….

                     

          }

           

          在新的代碼中,我們引入了一個變量PARAM_INDEX,并賦予一個初始值(這里是零),借助于對這個變量,我們消除了硬編碼常量的值帶來的那些弊端。同樣插入一個新的常量,現在我們只需要增加新的內容,而不需要改變原有的代碼了。代碼如下:

           

          public class A {

                     

                      …….

                      private static int PARAM_INDEX = 0;

                      private static final int PARAM_1 = PARAM_INDEX++;

                      private static final int PARAM_2 = PARAM_INDEX++;

                      private static final int PARAM_NEW = PARAM_INDEX++;  // New Constant

                      private static final int PARAM_3 = PARAM_INDEX++;

                      private static final int PARAM_4 = PARAM_INDEX++;

                      private static final int PARAMS_COUNT = PARAM_INDEX;

                      …….

                     

          }

           

          posted on 2007-04-10 17:19 Jini 閱讀(1556) 評論(4)  編輯  收藏 所屬分類: JDK相關

          評論:
          # re: 初始化有序常量的一點小技巧 2007-04-10 18:25 | TiGERTiAN
          呵呵,有意思  回復  更多評論
            
          # re: 初始化有序常量的一點小技巧 2007-04-11 10:40 | itspy
          是個有用的小技巧。  回復  更多評論
            
          # re: 初始化有序常量的一點小技巧 2007-04-11 11:16 | Web 2.0 技術資源
          嘿嘿!~   回復  更多評論
            
          # re: 初始化有序常量的一點小技巧 2012-07-23 10:58 | GrimRaider
          贊!  回復  更多評論
            
          主站蜘蛛池模板: 绥芬河市| 德安县| 西吉县| 华安县| 互助| 綦江县| 德阳市| 满洲里市| 衡阳市| 石棉县| 丽水市| 樟树市| 宁都县| 诸城市| 长泰县| 桦甸市| 海淀区| 桃园县| 保山市| 柯坪县| 措美县| 龙南县| 锡林浩特市| 南昌市| 滦平县| 新宁县| 孙吴县| 铜梁县| 孝义市| 万宁市| 永宁县| 朔州市| 长汀县| 沅陵县| 木里| 合水县| 凉城县| 乌拉特后旗| 青海省| 太白县| 辽阳市|