黑马程序员技术交流社区
标题:
使用泛型的代码?
[打印本页]
作者:
yedong07a
时间:
2013-5-10 12:19
标题:
使用泛型的代码?
本帖最后由 yedong07a 于 2013-5-11 11:47 编辑
public class GenericTest {
public static void main(String[] args) throws Exception {
ArrayList<Integer> collection3 = new ArrayList<Integer>();
collection3.getClass().getMethod("add", Object.class).invoke(collection3, "abc");
// 类的字节码得到方法,接收对象类型,调用方法作用于某个对象,这个?
System.out.println(collection3.get(0));
printCollection(collection3);
Method applyMethod = GenericTest.class.getMethod("applyVector",Vector.class);
Type[] types = applyMethod.getParameterTypes();
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);
}
}
}
复制代码
加注释的地方,听的不太清楚,不理解
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2