此代码:为什么新的Base()不能传递给<?扩展Base>?
public class Base<E> {
static void main(String[] args) {
Base<? extends Base> compound = new Base<Base>();
compound.method(new Base());
} //^error
void method(E e) { }
}
给出了这样的编译错误:
Error:(4, 17) java: method method in class Base<E> cannot be applied to given types;
required: capture#1 of ? extends Base
found: Base
reason: actual argument Base cannot be converted to capture#1 of ? extends Base by method invocation conversion
据我了解,E
变得? extends Base
,东西延伸Base
。那么,为什么new Base()
无法通过?
有了''方法之前,任何物体都可以通过:)不过,我的问题不是如何让它编译。我善意地要求解释它为什么不编译:) –
2014-12-13 14:51:23