无法将抽象方法声明为私有
我想这样做,但我不能。这是我的情景和理性。我有一个用于测试用例的抽象类,它有一个名为test()的抽象方法。test() 方法由子类定义;它将使用逻辑实现某个应用程序,例如 .我不希望直接调用 test() 方法,我希望超类调用 test() 方法,而子类可以调用调用此方法的方法(并且还执行其他工作,例如在执行测试之前设置当前日期时间)。示例代码:CRMAppTestCase extends CompanyTestCase
public abstract class CompanyTestCase {
//I wish this would compile, but it cannot be declared private
private abstract void test();
public TestCaseResult performTest() {
//do some work which must be done and should be invoked whenever
//this method is called (it would be improper to expect the caller
// to perform initialization)
TestCaseResult result = new TestCaseResult();
result.setBeginTime(new Date());
long time = System.currentTimeMillis();
test(); //invoke test logic
result.setDuration(System.currentTimeMillis() - time);
return result;
}
}
然后扩展这个....
public class CRMAppTestCase extends CompanyTestCase {
public void test() {
//test logic here
}
}
然后叫它....
TestCaseResult result = new CRMAppTestCase().performTest();