下面的代码对我来说是完全合理的 - 它是关于添加某种类型的元素,它是类型T的超类型而类型S绝对是超类型,那么为什么编译器拒绝将'element'添加到集合中?
class GenericType<S,T extends S>{
void add1(Collection<? super T> col ,S element ){
col.add(element); // error
// The method add(capture#9-of ? super T) in the type
// Collection<capture#9-of ? super T> is not applicable for the arguments (S)
}
}
Run Code Online (Sandbox Code Playgroud)
比如一个例子,如果A <- B <- Cwhere <-表示超类型,那么if S = B和T = Cyou不能将一个实例添加S到一个集合中T.
超类型T可以是超类型或另一种超类型的子类型T(在这种情况下S).