package softspractice;
/*
* 需求:获取一段程序运行的时间
* 原理:获取程序开始或结束的时间相减即可
*
* 获取时间:System.currentTimeMillis();
*
* 当代码完成优化后 就可以解决这类问题
* 这种方式,模板方法设计模式
*
* 什么事模板方法设计呢?
* 在定义功能时,功能一部分是确定的,但是有一部分是不确定的 而确定的部分在使用不确定的部分
* 那么这时将就不确定的部分暴漏出去 由这类的 子类去完成
*/
public class TemplateDemo {/**
- @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//GetTime gt = new GetTime();
SubTime gt = new SubTime();
gt.getTime();
}
}
abstract class GetTime{public final void getTime(){
long start = System.currentTimeMillis(); runcode();//写你需要获取运行时间的代码 long end = System.currentTimeMillis(); System.out.println("运行时间:"+(end-start)+"s");
}
public abstract void runcode();- @param args
}
class SubTime extends GetTime{
public void runcode(){
for(int x=0 ;x<4000 ;x++){
System.out.println(x);
}//写你需要获取运行时间的代码 运行子类
}
}