1 //模板方法模式 2 abstract class time{ 3 public final void getTime() {//不让复写 4 long start = System.currentTimeMillis(); 5 6 runcode(); 7 8 long end = System.currentTimeMillis(); 9 System.out.println("毫秒" + (end - start)); 10 } 11 12 public abstract void runcode(); 13 } 14 15 class Elem extends time{ 16 public void runcode() { 17 for (int i = 0; i < 1000; i++) { 18 System.out.println(i); 19 } 20 } 21 } 22 23 public class CurrentTime { 24 public static void main(String[] args) { 25 // GetTime gt = new GetTime(); 26 Elem gt = new Elem(); 27 gt.getTime(); 28 } 29 }