gpt4 book ai didi

java - 为什么我不能将子项列表传递给接受 List< 的方法?扩展父>?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:31 25 4
gpt4 key购买 nike

我有一个名为 FamilyHistoryPersonModel 的类扩展名为 PersonModel 的类.

    MapValue mapValue = new MapValue("relatives",
new GenericType<FamilyHistoryPersonModel>() {},
new GenericType<List<FamilyHistoryPersonModel>>() {} //error here
);

构造函数:

    private MapValue(String pluralUrl, GenericType<? extends PersonModel> singleResultGenericType, GenericType<List<? extends PersonModel>> listResultGenericType) {
this.pluralUrl = pluralUrl;
this.singleResultGenericType = singleResultGenericType;
this.listResultGenericType = listResultGenericType;
}

错误:

cannot find symbol
symbol : constructor MapValue(java.lang.String,<anonymous com.sun.jersey.api.client.GenericType<com.models.FamilyHistoryPersonModel>>,<anonymous com.sun.jersey.api.client.GenericType<java.util.List<com.models.FamilyHistoryPersonModel>>>)
location: class com.rest.v2.resource.CreatePersonModelIntegrationTest.MapValue

为什么会出现此错误?我如何解决它?我不想更改构造函数以接受 GenericType<List<FamilyHistoryPersonModel>> listResultGenericType

我正在使用 JDK 1.6

最佳答案

就像一个List<Dog>不是 List<Animal>即使Animal子类 Dog , 一个 GenericType<List<FamilyHistoryPersonModel>>不是 GenericType<List<? extends PersonModel>> ,即使是 List<FamilyHistoryPersonModel>List<? extends PersonModel> .

解决方案是在顶级泛型类型参数处提供一个通配符。在MapValue构造函数,改变参数从

GenericType<List<? extends PersonModel>> listResultGenericType

GenericType<? extends List<? extends PersonModel>> listResultGenericType

我假设您还需要更改 this.listResultGenericType 的类型匹配。

关于java - 为什么我不能将子项列表传递给接受 List< 的方法?扩展父>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24875816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com