黑马程序员技术交流社区

标题: 反射练习 [打印本页]

作者: fmi110    时间: 2015-10-1 15:44
标题: 反射练习
a
  1. import java.lang.reflect.Method;
  2. import java.util.ArrayList;


  3. public class T9 {

  4.         /**
  5.          * 向泛型为Integer的ArrayList中添加一个String类型元素
  6.          * @throws Exception
  7.          * @throws NoSuchMethodException
  8.          */
  9.         public static void main(String[] args) throws NoSuchMethodException, Exception {
  10.                 ArrayList<Integer> al = new ArrayList<Integer>();
  11.                 Class cl = al.getClass();
  12.                 Method add = cl.getMethod("add", Object.class);
  13.                 add.invoke(al, "hell java");
  14.                 System.out.println(al);
  15.         }

  16. }
复制代码





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