我在下面收到错误。
层次结构: GeneralBudgetBean - > RRBudgetBean
它们包含具有以下层次结构的数据成员: GeneralBudgetYearBean - > RRBudgetYearBean
父类 - GeneralBudgetBean.java
// Data member
private List<GeneralBudgetYearBean> budgetYearBeans;
// Getters/setters
public List<GeneralBudgetYearBean> getBudgetYearBeans() {
return budgetYearBeans;
}
public void setBudgetYearBeans(List<GeneralBudgetYearBean> budgetYearBeans) {
this.budgetYearBeans = budgetYearBeans;
}
扩展类 - RRBudgetBean.java
public class RRBudgetBean extends GeneralBudgetBean {
//...
public void initRRBudgetBean()
{
this.setBudgetYearBeans(new ArrayList<RRBudgetYearBean>());
}
}
错误:
The method setBudgetYearBeans(List<GeneralBudgetYearBean>) in the type GeneralBudgetBean is not applicable for the arguments ArrayList<RRBudgetYearBean>
换句话说,错误是:
Method method(List<Bean>) not applicable for arguments ArrayList<SubBean>.
答案 0 :(得分:1)
从教程(http://docs.oracle.com/javase/tutorial/java/generics/inheritance.html):
Box<Integer>
和Box<Double>
不是Box<Number>
的子类型。
在这种情况下,您必须将方法签名更改为
public void setBudgetYearBeans(List<? extends GeneralBudgetYearBean> budgetYearBeans) {
this.budgetYearBeans = budgetYearBeans;
}
关于通配符的http://docs.oracle.com/javase/tutorial/java/generics/wildcardGuidelines.html文章可能有用。
答案 1 :(得分:-2)
更改您的
setBudgetYearBeans(List<GeneralBudgetYearBean> budgetYearBeans)
到
setBudgetYearBeans(ArrayList<GeneralBudgetYearBean> budgetYearBeans)
通常,最好使方法参数类型更具体,并且方法返回值更一般。