黑马程序员技术交流社区

标题: 4、ArrayList<Integer> list = new ArrayList<Integer>(); 在这个泛型... [打印本页]

作者: huburt    时间: 2016-5-20 16:30
标题: 4、ArrayList<Integer> list = new ArrayList<Integer>(); 在这个泛型...
public static void main(String[] args) {
                // 定义Integer数组
                ArrayList<Integer> list = new ArrayList<Integer>();
                // 需要存入的String
                String a = "abc";
                // catch异常
                try {
                        // 利用反射获取到add方法
                        // 泛型的作用层在编译时。而反射直接获得了add方法的字节码跳过编译层在运行时直接添加。这样就骗过了编译。
                        Method method = list.getClass().getMethod("add", Object.class);
                        method.invoke(list, a);
                } catch (Exception e) {
                        e.printStackTrace();
                }
                System.out.println(list);
        }




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