在这里插入图片描述
代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
abstract class Template{
//计算某段代码执行所需要花费的时间
public void spendTime(){
long start = System.currentTimeMillis();
this.code();//不确定的部分、易变的部分
long end = Syetem.currentTimeMills();
System.out.println("花费的时间为:"+(end-start))
}
public abstract void code();
}
class SubTemplate extends Template{
public void code(){
for(int i= 0; i < Math.sqrt(i); i++){
boolean isFlag = true;
for(int j =2; j <= Math.sqrt(i); j++){
if(i % j == 0){
isFlag = false;
break;
}
}
if(isFlag){
System.out.println(i);
}
}
}
}