你好我的Java有點生疏,所以請耐心等待。我的任務是減少研究項目中重複代碼的數量,因此我認爲我無法在此處發佈任何代碼。但基本上,我有大約20個不同類別的大致相同的方法(測試方法),我一直在嘗試兩種不同的方法來解決這個問題,但我遇到了每個問題。將變量傳遞到使用其他方法或繼承的方法
我發現,以減少重複第一種方式是,以除去包含在所有的試驗方法初始變量並將其放置在一個單獨的方法(製備方法)類的超類的內部和具有它稱爲測試方法。這個解決方案的問題不是在準備方法中聲明的所有變量將保持本地狀態,並且一旦在另一個方法中調用該方法時就會被擦除?
我的第二個想法是讓超類的所有變量字段,並讓它們被子類繼承。這個解決方案几乎可以工作,除了其中一個變量IFile importedFile = importFile(file);
是創建這些變量所必需的變量之一,它必須包含拋出異常所包含的任何內容,我不相信你可以用類來完成。
我一直希望有人能夠用這些解決方案之一指出我正確的方向,或者可能會提出另一個我一直無法找到的解決方案。
我忘了提及的一點是,除了初始變量之外,每種測試方法在寫入測試方法時略有不同。 編輯:如果不是這樣的話,我會把這個方法拉進超類,並且完成它。
編輯:下面是測試方法,我使用了超類的部分,
// method inside of the subclass
public void test() throws Exception {
// variables removed and placed in AbstractTest
for (int i = 0; i < expectedExitNodeCount; i++) {
if (markerFields.peekFirst().equalsIgnoreCase("EXPOSED_EXIT")) {
expectedExitNodes.add(new CTrueExitNode());
} else {
fromLine = Integer.parseInt(markerFields.removeFirst().trim());
fromCol = Integer.parseInt(markerFields.removeFirst().trim());
toLine = Integer.parseInt(markerFields.removeFirst().trim());
toCol = Integer.parseInt(markerFields.removeFirst().trim());
length = length(ast, fromLine, fromCol, toLine, toCol);
assertTrue(length > 0);
ICFlowNode expectedExit = findNode(ast, ICFlowNode.class, fromLine, fromCol, length);
assertNotNull(expectedExit);
expectedExitNodes.add(expectedExit);
}
}
// additional code omitted from method
}
// Superclass the variables have been placed in.
public abstract class AbstractTestCase extends WorkTest {
protected File file;
protected String markerText;
public AbstractTestCase(String name, VPG< ? , ? , ? > vpg) {
super(name, vpg);
}
protected void prepare() throws Exception {
// Variables used in the test method
IFile importedFile = importFile(file);
project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
CVPG.getInstance().ensureVPGIsUpToDate();
CTranslationUnit ast = CVPG.getInstance().acquireTransientAST(
ResourceUtil.getFilenameForIFile(importedFile));
LinkedList<String> markerFields = MarkerUtil.parseMarker(markerText);
int fromLine = Integer.parseInt(markerFields.removeFirst().trim());
int fromCol = Integer.parseInt(markerFields.removeFirst().trim());
int toLine = Integer.parseInt(markerFields.removeFirst().trim());
int toCol = Integer.parseInt(markerFields.removeFirst().trim());
int length = length(ast, fromLine, fromCol, toLine, toCol);
assertTrue(length > 0);
IASTNode node = findNode(ast, IASTNode.class, fromLine, fromCol, length);
assertNotNull(node);
Integer expectedExitNodeCount = Integer.parseInt(markerFields.removeFirst().trim());
Set<ICFlowNode> expectedExitNodes = new HashSet<ICFlowNode>();
}
protected void test() throws Exception {
//Considered making test a inherited method
}
}
你面臨的例外是什麼? –
這將很難幫助刪除無代碼的輕微差異的重複。請求權限發佈它或提出一個遵循相同模式的假示例。 –
「每種測試方法略有不同」...有時[Template method pattern](http://en.wikipedia.org/wiki/Template_method)可以處理這種性質的重複。 – ajb