測試程序應該這樣寫

          大家都知道,為了測試,開發人員經常在代碼中加入System.out.println()這樣的測試語句,但是這樣做并不好,尤其是將打印語句寫在循環中的時候,更加影響性能。正確的方法應當是引入一個包裝器方法。

          /*
           *@author 我為J狂 建立日期 2007-5-17
           *
           
          */


          public class Test
          {
              
          public static final int DEBUG_MODE = 1;

              
          public static final int RUN_MODE = 2;

              
          public static void calculationWithPrint(int logMode)
              
          {
                  
          double someValue = 0D;
                  
          for (int i = 0; i < 100; i++)
                  
          {
                      someValue 
          = someValue + i;
                      myPrintMethod(logMode, someValue);
          //不要直接寫System.out.prinltn(),否則很影響性能。
                  }

              }


              
          public static void myPrintMethod(int logMode, double value)
              
          {

                  
          if (logMode > Test.DEBUG_MODE)
                  
          {
                      
          return;
                  }


                  System.out.println(value);
              }


              
          public static void main(String[] n)
              
          {
                  Test.calculationWithPrint(Test.RUN_MODE);
          //運行模式,不執行System.out.println();
                  Test.calculationWithPrint(Test.DEBUG_MODE);//調試模式,執行System.out.println();
              }

          }



          posted on 2007-05-17 18:50 我為J狂 閱讀(277) 評論(0)  編輯  收藏 所屬分類: JavaSE


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(11)

          隨筆分類(48)

          文章分類(29)

          常去逛逛

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 花莲县| 沙坪坝区| 清徐县| 旺苍县| 会泽县| 博乐市| 临高县| 英吉沙县| 常山县| 济南市| 张家港市| 绥宁县| 观塘区| 进贤县| 斗六市| 莆田市| 衡东县| 阳东县| 商南县| 石棉县| 深泽县| 炎陵县| 平泉县| 彩票| 酒泉市| 钟祥市| 自治县| 昌邑市| 灵武市| 民权县| 安溪县| 盐池县| 平定县| 贺兰县| 额济纳旗| 平邑县| 当雄县| 白城市| 湄潭县| 化隆| 中宁县|