Java 为什么可以';我是否将子对象列表传递给接受list<;的方法;?扩展父级>;?
我有一个名为Java 为什么可以';我是否将子对象列表传递给接受list<;的方法;?扩展父级>;?,java,generics,Java,Generics,我有一个名为FamilyHistoryPersonModel的类,它扩展了名为PersonModel的类 MapValue mapValue = new MapValue("relatives", new GenericType<FamilyHistoryPersonModel>() {}, new GenericType<List<FamilyHistoryPersonModel>>() {} //e
FamilyHistoryPersonModel
的类,它扩展了名为PersonModel
的类
MapValue mapValue = new MapValue("relatives",
new GenericType<FamilyHistoryPersonModel>() {},
new GenericType<List<FamilyHistoryPersonModel>>() {} //error here
);
MapValue MapValue=新的MapValue(“亲戚”,
新的GenericType(){},
新建GenericType(){}//此处有错误
);
建造商:
private MapValue(String pluralUrl, GenericType<? extends PersonModel> singleResultGenericType, GenericType<List<? extends PersonModel>> listResultGenericType) {
this.pluralUrl = pluralUrl;
this.singleResultGenericType = singleResultGenericType;
this.listResultGenericType = listResultGenericType;
}
私有映射值(字符串pluralUrl,GenericType就像列表
不是列表
即使动物
子类狗
,aGenericType
也不是GenericType你在使用哪个JDK?仅供参考-我认为这是“固定的”在JDK8中:。这是可行的,但我不明白为什么我必须这样做。为什么列表a列表?@tieTYT-“这是可行的,但我不明白为什么我必须这样做。为什么列表不是列表(在我上面的评论中链接到)是这样问的。@tieTYT Java泛型类型是不变的,您希望它们是协变的。有关更多详细信息,请参阅。@RGETMAN ok,这很有帮助。这解释了为什么它们不一样。但我仍然不明白为什么
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<? extends PersonModel>> listResultGenericType
GenericType<? extends List<? extends PersonModel>> listResultGenericType