在主类中,我初始化了一些 Filters 并将它们添加到ArrayList healthyFoodlist 中。然后,我使用此ArrayList初始化 healthyFood 过滤器。
public static void main(String[] args) {
Main m = new Main();
Filter veggieFilter = m.new SimpleFilter("vegetable");
Filter fruitFilter = m.new SimpleFilter("fruit");
Filter noPoison = m.new NotFilter("poison");
ArrayList<Filter> healthyFoodlist = new ArrayList<>();
healthyFoodlist.add(veggieFilter);
healthyFoodlist.add(fruitFilter);
healthyFoodlist.add(noPoison);
Filter healthyFood = m.new OrFilter(healthyFoodlist);
}
然后在我的 OrFilter 类中,我尝试将 healthyFoodlist 中的第一个过滤器分配给名为 filter1 的私有过滤器。
class OrFilter implements Filter {
private Filter filter1;
private Filter filter2;
private Filter filter3;
OrFilter(ArrayList a) {
filter1=a.get(0);
}
}
filter1 = a.get(0);在其中显示“不兼容的类型,必需:过滤器,找到:对象”
在第一段代码中,我将过滤器添加到 healthyFoodlist 中,而不是对象中,为什么会出现此错误?