我有一个名为 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/