對於一個Java類,我們正在編寫數據結構類,我們需要編寫一個測試程序類以便與它們一起使用。我正在尋求額外的功勞,並試圖編寫一個單獨的測試程序類,我可以擴展或傳遞一個塊以供任何測試使用。編寫一個類,以便它可以擴展或傳遞塊JAVA
是否可以傳遞一個方法的代碼塊來運行?如果這不可行或不實際,編寫一門課程的最佳方法是什麼?
--CODE--
package Lab1;
/**
* @author $RMDan
* @date 10-Sep-2012
* @class Tester
* @desc A Tester class for testing Java classes
* For use in Itec 251
*/
import java.io.*;
import java.util.*;
public class Tester {
//members
private String logS;
//Constructors
//Default to start == true
public Tester(){
this(true);
}
public Tester(Boolean start){
if(start == true){
this.start();
}
}
public final int start(){
int exit;
exit = test();
//this.displayLog();
this.exportLog("Test_Employee.Log");
return exit;
}
//Change the contents of this method to perform the test
private int test(){
return 0;
}
private void log(String message){
this.log(message,true);
}
private void log(String message, boolean display){
this.logS += message + "\n";
if(display==true){
System.out.println(message);
}
}
private void displayLog(){
System.out.print(this.logS);
}
private void exportLog(String file){
try{
String output = this.logS;
output = output.replaceAll("\n", System.getProperty("line.separator"));
try (BufferedWriter out = new BufferedWriter(new FileWriter(file + ".txt"))) {
out.write(output);
}
}
catch(IOException e){
System.out.println("Exception ");
}
}
}
注:最終的start()方法的聲明是沒有閉嘴的編譯器。
這完全適用於我的目的,我會更加深刻的印象。這也向我展示瞭如果我願意的話,我可以如何覆蓋測試方法。謝謝 – RMDan