黑马程序员技术交流社区

标题: 使用泛型的代码? [打印本页]

作者: yedong07a    时间: 2013-5-10 12:19
标题: 使用泛型的代码?
本帖最后由 yedong07a 于 2013-5-11 11:47 编辑

  1. public class GenericTest {
  2.     public static void main(String[] args) throws Exception {
  3.         ArrayList<Integer> collection3 = new ArrayList<Integer>();
  4.         collection3.getClass().getMethod("add", Object.class).invoke(collection3, "abc");
  5.         // 类的字节码得到方法,接收对象类型,调用方法作用于某个对象,这个?
  6.         System.out.println(collection3.get(0));

  7.         printCollection(collection3);

  8.         Method applyMethod = GenericTest.class.getMethod("applyVector",Vector.class);
  9.         Type[] types = applyMethod.getParameterTypes();
  10.         ParameterizedType pType = (ParameterizedType) types[0];
  11.         System.out.println(pType.getRawType());
  12.         System.out.println(pType.getActualTypeArguments()[0]); // 通过反射的方式拿到泛型里面的?类型,这里
  13.         }
  14.         public static void applyVector(Vector<Date> v1) {
  15.         }
  16.          public static void printCollection(Collection<?> collection) {
  17.          // collection.add(1);
  18.          System.out.println(collection.size());
  19.          for (Object obj : collection) {
  20.             System.out.println(obj);
  21.          }
  22.          }
  23. }
复制代码
加注释的地方,听的不太清楚,不理解




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2