2015-10-30 22 views
2

我在Java中有一個接口,它有許多特定於特定類型測試的參數。然後,我有一個實用程序類,其功能可以用於許多不同的項目,我想要更改的唯一功能是實用程序功能中使用的參數。參數化Java類以獲取任意多個接口

我嘗試的解決方案是使測試工具類參數化爲我希望它實現的接口類型。我希望能夠向班級發送一個或多個界面。像圖所示:

public class TestsetA extends TestUtilityFunctions<ParametersA,...> {} 

public class TestUtilityFunctions<T...> implements T... {} 

然而,這是給我的錯誤:Can not refer to type parameter T as supertype.

如何我Java泛型實現這一點?

UPDATE: 的用例即時試圖解決的是以下幾點:

public abstract class TestUtilityFunctions<T implements CommonProperties> { 

    protected void initRestAssured() { 
     RestAssured.port = T.PORT; 
     RestAssured.basePath = T.BASE_URL; 
}} 

public class MyATests extends TestUtilityFunctions<ParametersForA> { 

} 

public class MyBTests extends TestUtilityFunctions<ParametersForB> { 

} 
+1

你不能(仿製藥不能添加的方法來傳遞到一個班級,他們只能改變他們的工作類型)。用一個小例子來解釋你的用例,說明/爲什麼,人們可能能夠找到一個工作思路。 – zapl

+0

http://stackoverflow.com/questions/26400049/generic-super-class-in-java – Rumoku

+0

我更新了一個更好的例子,顯示我的意圖,我希望更好。 – user3139545

回答

1

我想你想在一個實例

public abstract class TestUtilityFunctions<T implements CommonProperties> { 
    private final T props; 
    protected TestUtilityFunctions(T props) { 
     this.props = props; 
    } 
    protected void initRestAssured() { 
     // are PORT and BASE_URL static? 
     // if no, they shouldn't be capitals (use getPort() and getBaseUrl()) 
     // if yes, this is bad design 
     RestAssured.port = props.PORT; 
     RestAssured.basePath = props.BASE_URL; 
    } 
}