A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yedong07a 中级黑马   /  2013-5-10 12:19  /  1042 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 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. }
复制代码
加注释的地方,听的不太清楚,不理解

点评

希望楼主可以把问题阐述得再具体一点,谢谢  发表于 2013-5-10 21:59

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马