黑马程序员技术交流社区

标题: 关于反射和泛型 [打印本页]

作者: 木木三    时间: 2015-9-9 17:09
标题: 关于反射和泛型
字节码文件类型需要加泛型么?Eclipse总是跳出这个!,看着真不舒服。
但是视频里面也没看到加了泛型限定啊,限定是谁谁谁的类类型这种。
  1. import java.lang.reflect.Method;
  2. import java.util.ArrayList;
  3. public class  {
  4.         public static void main(String[] args){
  5.                 ArrayList<Integer> al = new ArrayList<Integer>();
  6.                 //?Class is a raw type. References to generic type Class<T> should be parameterized
  7.                 Class c = al.getClass();//获取字节码
  8.                 try{
  9.                         //?Type safety: The method getMethod(String, Class...) belongs to the raw type Class.
  10.                         //?References to generic type Class<T> should be parameterized
  11.                         Method me = c.getMethod("add", Object.class);//获取add方法
  12.                         me.invoke(al, "Hello Reflect!");//运行方法
  13.                 }
  14.                 catch(Exception e){
  15.                         e.printStackTrace();
  16.                 }
  17.                 System.out.println(al);
  18.         }
  19. }
复制代码





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