简单类:
class Box<T> {
private T t;
public Box(T t) {
this.t = t;
}
public void put(T t) {
this.t = t;
}
}
尝试执行传递 Object 实例的 put() 方法
Box<?> box = new Box<String>("abc");
box.put(new Object());
编译器指出一个错误:
The method put(capture#1-of ?) in the type Box<capture#1-of ?> is not applicable for the arguments (Object)
编译器实际上不知道期望什么类型,但有一件事是肯定的——它将是一个 Object 或它的子类。为什么会引发错误呢?
谢谢你