gpt4 book ai didi

java - 具有泛型类型参数的方法可以传递泛型类型参数

转载 作者:行者123 更新时间:2023-12-01 22:58:55 24 4
gpt4 key购买 nike

我的 Customer 类中有带有 getter 和 setter 的实例

private Collection<CustomersDetails> customerDetails = new ArrayList<CustomersDetails>();

public Collection<CustomersDetails> getCustomerDetails() {
return customerDetails;
}
public void setCustomerDetails(Collection<CustomersDetails> customerDetails) {
this.customerDetails = customerDetails;
}

但是当我试图通过时

CustomersDetails customersDetails = new CustomersDetails();

customersDetails.set....
customersDetails.set....


Customer customer = new Customer();
customer.setCustomerDetails(customersDetails);

它给了我错误

The method setCustomerDetails(Collection<CustomersDetails>) in the type Customer is not applicable for the arguments (CustomersDetails) 

为什么会这样,当我的 Collection 是 CustomersDetails 类型时?

最佳答案

您的Customer类有一个方法

public void setCustomerDetails(Collection<CustomersDetails> customerDetails) {
this.customerDetails = customerDetails;
}

您正尝试使用 CustomersDetails 类型的参数来调用它。一个CustomersDetails不是Collection<CustomersDetails> 。将元素包装在 List 中或其他Collection .

类似于

customer.setCustomerDetails(Arrays.asList(customersDetails));

关于java - 具有泛型类型参数的方法可以传递泛型类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23641736/

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