- f2(Holder<?> holder)的参数holder可以捕获到原生类型中的参数类型,但是自己却不知道;
- f2(Holder<?> holder)捕获到的参数类型可以转换成f1(Holder<T> holder)中的确切类型;
- 也就是说,无界通配符可以捕获到参数类型,但是不转换就不知道参数类型是什么;
package chapter15._10._4;
import chapter15._10._1.Holder;
public class CaptureConversion {
static <T> void f1(Holder<T> holder) {
T t = holder.get();
System.out.println(t.getClass().getSimpleName());
}
static void f2(Holder<?> holder) {
f1(holder);
}
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Holder raw = new Holder<Integer>(1);
// f1(raw);
f2(raw);
Holder rawBasic = new Holder();
rawBasic.set(new Object());
f2(rawBasic);
Holder<?> wildcarded = new Holder<Double>(1.0);
f2(wildcarded);
}
}