接口中的泛型类型与实现中的泛型类型
Generic Type from Interface with Generic Type in Implementation
我在 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
}
}
我不确定这是否真的是您想要做的,但这种方式可行:
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
}
}
我在 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
}
}
我不确定这是否真的是您想要做的,但这种方式可行:
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
}
}