【发布时间】:2017-03-20 11:30:02
【问题描述】:
我有一个非常常见的用例与 JAVA 中的泛型有关,我无法集中精力,这怎么可能。
以下示例表明,无法在接口中创建泛型类型以进一步将其作为泛型类型传递到实现方法中。
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
}
}
【问题讨论】:
-
首先,您的界面没有使用其他两种类型。其次,您将 List> 传递给 MyInterface 并在我的抽象类中分别在方法中使用 List
和 List ,这是错误的,您应该在任何地方都有相同的类型,无论是 O 还是 I 等。
标签: java generics interface abstract