本帖最后由 海东青 于 2015-4-8 16:17 编辑
import java.lang.reflect.Method;
import java.util.ArrayList;
public class test {
/**
* @param args 杨
* @throws Exception
* @throws SecurityException
* 两种做法,考察的是反射和泛型综合,具体来说是绕过编译时的泛型检查
*/
public static void main(String[] args) throws SecurityException, Exception {
// TODO Auto-generated method stub
ArrayList<Integer> list=new ArrayList<Integer>();
list.add(1);
Class c=list.getClass();
Method method=c.getDeclaredMethod("add", Object.class);
method.invoke(list,"nihao");
System.out.println(list);
System.out.println("-----------------------------------------------");
System.out.println("l1定义为integer型,l2不限制泛型");
ArrayList<Integer> l1=new ArrayList<Integer>();
ArrayList l2=l1;
l2.add("nihao");
System.out.println(l1);
}
|