您的位置:首页 > 其它

绕过泛型,通过反射把 String 添加到 List<Integer> 中

2016-09-06 15:31 651 查看
public class ArrayListDemo {

public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
List<Integer> list = new ArrayList<>();

// list.add(1);
// list.add(2);
// list.add("hello");

Class c = list.getClass();
Method method = c.getMethod("add",Object.class);
method.invoke(list,"hello");
method.invoke(list,"world");
method.invoke(list,"liwei");

System.out.println(list);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  反射
相关文章推荐