美文网首页java复习
2020-08-02通过反射向Integer数组中添加字符串

2020-08-02通过反射向Integer数组中添加字符串

作者: 智障猿 | 来源:发表于2020-08-12 16:10 被阅读0次
    public class Demo {
        public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
            ArrayList<Integer> arrayList = new ArrayList<>();
            arrayList.add(15);
            Class<? extends ArrayList> aClass = arrayList.getClass();
            Method add = aClass.getMethod("add", Object.class);
            add.invoke(arrayList,"hello");
            add.invoke(arrayList,"world");
            System.out.println(arrayList);
        }
    }
    

    相关文章

      网友评论

        本文标题:2020-08-02通过反射向Integer数组中添加字符串

        本文链接:https://www.haomeiwen.com/subject/eraprktx.html