ArrayList<String> al = new ArrayList<String>();
al.getClass().getMethod("add", Object.class).invoke(al, 3);
System.out.println(al.get(0));
上面的代码会报错: java.lang.Integer cannot be cast to java.lang.String
为什么下面的代码正常?
ArrayList<Integer> al = new ArrayList<Integer>();
al.getClass().getMethod("add", Object.class).invoke(al, "abc");
System.out.println(al.get(0)); |