子类参数化问题

Subclass parameterization issue

所以在 class 测试中,我正在创建 A 类型的 observableLists,其中 A 有子classes B 和 C。一个列表用于每个子class 的对象。但是,我收到以下错误:

The method foo(ObservableList<A>) in the type is not applicable for the arguments (ObservableList<B>). 

为什么会这样?由于 B 是 A 的子class,这不应该工作吗?

其他说明:

A、B 和 C 与 Test 在不同的包中。

这应该行不通。让我们用具体的例子。

foo(List<Number>)不接受List<Double>。如果您期望 List<Number>,您希望能够向其添加 Number。您不能将任何 Number 添加到 List<Double>,现在可以吗?

考虑以下可能的实现 - 假装它允许我们编译:

void foo(List<Number> list) {
   /// all lists have to have a 42
   list.add(new Integer("42"));
}

// later
List<Double> list = new ArrayList<>();
// oh man, we need to foo this list!
foo(list); // kaboom! when we add the integer to the double list!!

由于 B 是 A 的子类,B 可能比 A 具有更多的属性。这就是不匹配的原因。