测试程序应该这样写

大家都知道,为了测试,开发人员经常在代码中加入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狂 阅读(258) 评论(0)  编辑  收藏 所属分类: JavaSE


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(11)

随笔分类(48)

文章分类(29)

常去逛逛

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜