/* * Requirement: Get the running time of a program * Principle: Get the start and end times of the program and subtract them * * Get time: through System.currentTimeMillis(); * When the code is optimized, such problems can be solved * This method, the template method design pattern * * What is a template method * When defining a function, part of the function is deterministic, part is indeterminate, and the determinate part is using the indeterminate part * Then the uncertain part will be exposed at this time, and the subclass of the class will complete it * * * */ 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 < 4000; x++) { System.out.println(x); } } } public class TemplateDemo { public static void main(String args[]) { SubTime gt = new SubTime(); gt.getTime(); } }
-----------------------By chick