我想这样做,但我做不到.这是我的情景和理性.我有一个测试用例的抽象类,它有一个名为test()的抽象方法.test()方法由子类定义; 它将用某个应用程序的逻辑实现,例如CRMAppTestCase extends CompanyTestCase.我不希望直接调用test()方法,我希望超类调用test()方法,而子类可以调用一个调用它的方法(并做其他工作,例如设置当前例如,在执行测试之前的日期时间.示例代码:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后扩展这个....
public class CRMAppTestCase extends CompanyTestCase {
public void test() {
//test logic here
}
}
Run Code Online (Sandbox Code Playgroud)
然后叫它....
TestCaseResult result = new CRMAppTestCase().performTest();
Run Code Online (Sandbox Code Playgroud)
Jes*_*per 60
私有方法不是多态的(你不能继承它们),所以将私有方法抽象是没有意义的.使方法抽象意味着您必须在子类中覆盖和实现它,但由于您不能覆盖私有方法,因此您也不能将它们抽象化.
你应该做protected而不是private.
私有对你定义方法的类来说是私有的; 甚至子类也看不到私有方法.
| 归档时间: |
|
| 查看次数: |
23523 次 |
| 最近记录: |