測試程序應該這樣寫

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

          /*
           *@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);
          //運行模式,不執(zhí)行System.out.println();
                  Test.calculationWithPrint(Test.DEBUG_MODE);//調(diào)試模式,執(zhí)行System.out.println();
              }

          }



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


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


          網(wǎng)站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統(tǒng)計

          常用鏈接

          留言簿(11)

          隨筆分類(48)

          文章分類(29)

          常去逛逛

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 珲春市| 成武县| 调兵山市| 甘洛县| 中卫市| 翼城县| 宁化县| 嘉义市| 石台县| 大竹县| 日土县| 烟台市| 淳化县| 新疆| 桂平市| 营口市| 大冶市| 金乡县| 桃江县| 张掖市| 临夏市| 股票| 左贡县| 新和县| 万盛区| 广昌县| 南昌县| 白水县| 巢湖市| 金塔县| 泗洪县| 达日县| 盱眙县| 江达县| 财经| 涡阳县| 鱼台县| 宁陵县| 山阳县| 昌图县| 孟连|