1 public class TestTemplate { 2 public static void main(String[] args) { 3 new SubTemplate().spendTime(); 4 } 5 } 6 7 abstract class Template { 8 9 public abstract void code(); 10 11 public void spendTime() { 12 long start = System.currentTimeMillis(); 13 14 this.code(); 15 16 long end = System.currentTimeMillis(); 17 System.out.println("花费的时间为:" + (end - start)); 18 } 19 } 20 21 class SubTemplate extends Template { 22 23 public void code() { 24 boolean flag = false; 25 for(int i = 2;i <= 10000;i++){ 26 for(int j = 2;j <= Math.sqrt(i);j++){ 27 if(i % j == 0){ 28 flag = true; 29 break; 30 } 31 } 32 if(!flag){ 33 System.out.println(i); 34 } 35 flag = false; 36 } 37 } 38 }