//Vector<Date> v1 = new Vector<Date>();
通过反射来获得泛型到底用的是那个具体类型,知道后,我就可以用什么类型来填充。
例如:hibernate就用这种思想来实现了这个功能。
Method applyMethod = GenericTest.class.getMethod("applyVector", Vector.class);
Type[] types = applyMethod.getGenericParameterTypes();
ParameterizedType pType = (ParameterizedType)types[0];
System.out.println(pType.getRawType());
System.out.println(pType.getActualTypeArguments()[0]);
}
public static void applyVector(Vector<Date> v1){
}
<-------------------<?> 通配符-------------------------->
public static void printCollection(Collection<?> collection){
//collection.add(1);
System.out.println(collection.size());
for(Object obj : collection){
System.out.println(obj);
}
}
上面这个代码:
主要解决的问题:定义一个方法,该 方法用于打印出任意参数化类型的集合中的所有数据,该 方法如何定义
使用<?> 通配符可以引用其他各种参数化的类型,<?> 通配符定义的变量主要用作引用,可以调用与参数化无关的方法,不能调用与参数化有关的方法。
限定通配符的上边界
Vector<? extends Number> x=new Vector<Integer>()
限定<?> 通配符的下边界
Vector<? super Integer> x=new Vector<Byte>()
限定通配符总是包括自己。