繁体   English   中英

实现中的通用类型与接口的通用类型

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM