A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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);
        }

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马