反射练习之越过泛型检查

package ClassTest.ClassFieldsTest;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * FileName: ReflectDemo05
 * Author:   lps
 * Date:     2022/4/11 22:24
 * Sign:刘品水 Q:1944900433
 */
public class ReflectDemo05 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        //创建集合
        ArrayList<Integer> array = new ArrayList<>();
//        array.add(10);
//        array.add(20);
//        array.add(30);
//        array.add(Integer.valueOf("lps"));//XXX错误示范
        
        Class<? extends ArrayList> c = array.getClass();

        Method add = c.getMethod("add", Object.class);
        add.invoke(array,"刘品水");
        array.add(21);
        add.invoke(array,"岁");

        System.out.println(array);


    }
}

  

 

posted @ 2022-04-11 22:36  刘品水  阅读(33)  评论(0编辑  收藏  举报