[英]Generic Type from Interface with Generic Type in Implementation
我在JAVA中有一個非常常見的用例與Generics混在一起,我無法集中精力解決這種情況。
下面的示例顯示不可能在Interface中創建Generic Type以便在實現方法中進一步將其作為Generic Type傳遞。
interface MyInterface <T, I, O>
{
public T method(T arg);
}
abstract class MyAbstractClass <I , O> implements MyInterface<List<?>, I , O>
{
// This cause a syntax-failure! What is wrong with this?
// And how can I manage my Generics like this?
@override
public abstract List<O> method(List<I> arg);
}
class MyImplementation extends MyAbstractClass <String , Integer>
{
List<String> method(List<Integer> arg)
{
// ... implementation
}
}
我不確定這是否真的是您要執行的操作,但是這種方式可行:
interface MyInterface <T, I, O>
{
T method(I arg);
}
abstract class MyAbstractClass <O, I> implements MyInterface<List<O>, List<I> , O>
{
@Override
public abstract List<O> method(List<I> arg);
}
class MyImplementation extends MyAbstractClass <String , Integer>
{
@Override
public List<String> method(List<Integer> arg) {
// ... implementation
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.