繁体   English   中英

具有泛型和带界参数的Java接口

[英]Java interface with generics with bounded parameter

我有一个需要使用泛型的要求,因为客户端在使用我的服务时可以使用它们所需的数据类型。 代码如下:

public interface MyService<T extends Base> {

    void meth1(T type);
}

@Service("abc")
public class MyServiceImpl<T> implements MyService<T extends Base> {

    @Override
    public void meth1(T type) {
    }
}

其中Base是一个抽象类。 客户将使用它的子类来调用此服务。

public abstract class Base {
}

问题:实现类(MyServiceImpl)无法编译。 我不确定这里的问题。

客户端将使用此服务并推送其数据类型,该数据类型是Base的子类。 例如这样的事情:

    @Autowired
    @Qualifier("abc")
    private MyService<xx> service;


service.meth1(xx)  //where xx is subclass of Base

我在这里做错什么吗? 请提出建议。

类型绑定应在type参数的声明中:

public class MyServiceImpl<T extends Base> implements MyService<T>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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