抽象类获取时间

调用了windowsAPI

package yuwei.com;
/*
 * 需求:获取一段程序运行的时间
 * 原理:获取程序开始和结束的时间并相减
 * 
 * 获取时间:
 * System.currentTimeMillis()
 * 
 * 当代码完成优化后,就可以解决这类问题
 * 这种方式成为模板方法设计模式
 * 
 * 在定义功能时,功能的一部分是确定的
 * 
 * 但是有一部分是不确定的,那么就将不确定暴漏出去,由子类复写
 * 提高了程序的扩展性
 * 
 * */
abstract class GetTime{

    //强制不让复写
    public final void getTime() {
        long start = System.currentTimeMillis();

        runcode();

        long end = System.currentTimeMillis();
        System.out.println("毫秒:" + (end - start));
    }
    public abstract void runcode();
}
//
class SubTime extends GetTime
{
    public void runcode() {
        for(int x = 0;x < 100; x++) {
            System.out.print(x);
        }
    }
}
public class TemplateDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //父类指向子类对象
        GetTime gt = new SubTime();
        gt.getTime();
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_40051278/article/details/80488088