我有两个问题,他们可能是愚蠢的问题,但现在让我感到困惑。
问题1:
我有这段代码:
private List<Car> carList;
public void setData(List<Object> list)
{
this.carListist = list;
}
但在this.carList = list
我收到了一个编译错误:incompatible types, required: List<Car> but found List<Object>
我的问题是,为什么会出现这个错误? 是不是列出了Car类型的对象列表?那么为什么它不接受那里的对象?
问题2: 我将上面的代码更改为以下代码,错误消失了:
private List<Object> carList;
public void setData(List<Object> list)
{
this.carListist = list;
}
public Object getValueAt(int row, int column) {
Car car = carList.get(row); //Compiling error here
switch(column)
{
(...)
}
(..)
编译错误(在代码注释中签名)为incompatible types, required Car but found Object
我不明白的是,现在list被定义为一个对象列表。汽车是一个对象。为什么汽车作为一个对象不被接受?
无论如何要为问题1和2解决这个问题?
答案 0 :(得分:1)
这并不是因为每辆车都是一个对象,汽车列表也是一个对象列表,在你的例子中,如果对象列表包含船而不是汽车,那该怎么办....