此代碼:爲什麼新的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