类型完全匹配,但Java编译器无法为List <type>进行编译?

时间:2019-11-06 18:57:59

标签: java generics

private ResourceAssembler<? extends BasedEntity> resourceAssembler;
List<? extends BasedEntity> bases = userRepository.findAll();
return resourceAssembler.toResources(bases, User.class);  //<-----compile error

编译错误

Wrong 1st argument type. Found: 'java.util.List<? extends BasedEntity>', required: 'java.util.List<? extends BasedEntity>'

方法签名

public class ResourceAssembler<T extends BasedEntity> {
    public Resources<Resource<T>> toResources(List<T> list, Class entityClass) {}
}

这是怎么回事?


可以复制并粘贴到空项目中的最小代码示例。

class Main {

    ResourceAssembler<? extends BasedEntity> resourceAssembler;
    List<? extends BasedEntity> bases;

    void test() {
        resourceAssembler.toResources(bases);
        //         the error is here: ^~~~~
    }

}

class BasedEntity {}

class ResourceAssembler<T extends BasedEntity> {
    void toResources(List<T> list) {}
}

0 个答案:

没有答案