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

时间:2014-05-13 21:17:01

标签: java

我的客户

中有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) 

为什么会这样,当我的收藏是 CustomersDetails 类型??

1 个答案:

答案 0 :(得分:3)

您的Customer班级有方法

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

您正尝试使用CustomersDetails类型的参数调用它。 CustomersDetails不是Collection<CustomersDetails>。将元素换入List或其他Collection

这样的东西
customer.setCustomerDetails(Arrays.asList(customersDetails));