我試圖將實現DomainEntity的對象集合轉換爲實現DomainEntityDTO的對象集合。 DomainEntity對象提供了一個方法toDTO()來進行轉換。在Java中以通用方式轉換集合
這是我的代碼。 。
public class EntityCollectionConverter<T extends DomainEntityDTO, Y extends DomainEntity> {
public Collection<T> convert(Collection<Y> collection){
Collection<T> dtoList = new ArrayList<>();
for (DomainEntity domainObject : collection) {
DomainEntityDTO dto = domainObject.toDTO();
dtoList.add(dto); // Compiler: "T cannot be applied to DomainEntityDTO"
}
return dtoList;
}
}
線dtoList.add(dto);
無法編譯,因爲「T不能應用於DomainEntityDTO」
接口DomainEntity看起來是這樣的:
public interface DomainEntity {
Long getId();
<T extends DomainEntityDTO> T toDTO();
}
任何想法,我錯了?
' T toDTO()'沒有任何意義。它不知道'T'是什麼。 –
SLaks
讓接口DomainEntity {Long getId(); T toDTO(); }' –