Java泛型全面解析
1. 泛型方法与构造函数
泛型方法允许在方法级别定义类型参数。例如,调用泛型方法时可以指定实际类型参数:
// Specify that Integer is the actual type for the type parameter for m1()
t.<Integer>m1(iw1, iw2, "hello");
// Let the compiler figure out the actual type parameter for the m1() call
// using types for iw1 and iw2
t.m1(iw1, iw2, "hello"); // OK
静态泛型方法只能引用自己声明的类型参数,不能引用包含类的类型参数。以下是 WrapperUtil 类中的 copy 静态方法:
public static <T> void copy(Wrapper<T> source, Wrapper<? super T> dest) {
T value = source.get();
dest.set(value);
}
调用静态泛型方法时指定实际类型参数:
WrapperUtil.<Integer>copy(iw1, iw2);
<
超级会员免费看
订阅专栏 解锁全文
914

被折叠的 条评论
为什么被折叠?



