private Set<String> extractOfferKeysForAbstractOffers(List<AbstractOfferDto> selectedOffers) {
Set<String> offerKeys = new HashSet<String>();
for (AbstractOfferDto offer : selectedOffers) {
offerKeys.add(offer.getOfferKey());
}
return offerKeys;
}
private Set<String> extractOfferKeysForOffers(List<OfferDto> selectedOffers) {
Set<String> offerKeys = new HashSet<String>();
for (OfferDto offer : selectedOffers) {
offerKeys.add(offer.getOfferKey());
}
return offerKeys;
}
我想使用泛型,而不是使用几乎相同的方法,只是输入不同。我像这样创建它。
private <T> Set<String> extractOfferKeysForOffers(List<T> offers) {
Set<String> offerKeys = new HashSet<String>();
for (T offer : offers) {
offerKeys.add(offer.getOfferKey());
}
return offerKeys;
}
但问题是offer.getOfferKey()
不被认可。提供的唯一选项是AbstractOfferDto
或OfferDto
。
我如何在这个例子中使用泛型?