可以说我们有2个班级。空类Base和该类的子类Derived。
public class Base {}
public class Derived extends Base {}
然后在另一个类中有一些方法:
import java.util.Collection
public class Consumer {
public void test() {
set(new Derived(), new Consumer().get());
}
public <T extends Base> T get() {
return (T) new Derived();
}
public void set(Base i, Derived b) {
System.out.println("base");
}
public void set(Derived d, Collection<? extends Consumer> o) {
System.out.println("object");
}
}
这可以在Java 7中编译并成功运行,但不能在Java 8中编译。错误:
Error:(8, 9) java: reference to set is ambiguous
both method set(Base,Derived) in Consumer and
method set(Derived,java.util.Collection) in Consumer match
为什么在Java 7中有效,但在Java 8中无效?怎么可能<T extends Base> 永远匹配收集?
慕慕森
相关分类