Java クラスの場合、データ構造クラスを作成しており、それらに対応するテスター クラスを作成する必要があります。私は余分な信用を得るつもりで、テストに使用するために拡張またはブロックを渡すことができる単一のテスター クラスを作成しようとしています。
メソッドを実行するためにコードのブロックを渡すことは可能ですか? これが不可能または実用的でない場合、拡張できるようにクラスを作成する最良の方法は何ですか?
- コード -
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() メソッドの宣言の最後の部分は、コンパイラをシャットダウンするためにあります。