模版設計模式 記錄

模版設計模式的簡單記錄,以備忘.
內容比較簡單直接上代碼

public class TimeRecorder{
	pulic final void recordTime(){
		Long startTime = System.currentTimeMillis();
		this.code();
		Long endTime = System.currentTimeMillis();
		System.out.println("執行code代碼耗時:"+endTime-startTime);
	}
	//定義一個抽象函數用於給子類存放要測算執行時間的代碼.
	public abstract void code();
}  
  • 比如要測試輸出一百個數字java虛擬機要耗費多少時間代碼如下
class caculateOneToHandrad extends TimeRecorder{
	@OverWrite 
	public void code(){
		int i = 1;
		while(i<=100){
			System.out.println(i);
			i++;
		}
	}

	public static void main(String[] args){
		caculateOneToHandrad  coth = new caculateOneToHandrad ();
		coth.recodeTime();//此句即可打印出來執行代碼code中的邏輯所用的毫秒時間.
	} 
} 


 

 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章