从子类方法返回泛型和集合

时间:2020-02-13 18:18:41

标签: java generics collections subclass

使用Java进行了多年编程之后,我仍然不明白这一点!

定义了顶级类

public abstract class GenericCache<K, T, P> {
  public ArrayList<T> getAll() {}
  public T getSafe(K a_id) {}
}

子类是

public class SKUTypeCache<T extends SKUType, P> extends GenericCache<Integer, T, P> {
}

当我尝试使用它

SKUTypeCache l_cache = getTheCache();
for (SKUType t : l_cache.getAll()) {
}

我在for循环上收到编译错误:“不兼容的类型:无法将对象转换为SKUType”。为什么?使用getSafe()有类似的问题。

0 个答案:

没有答案