zoukankan      html  css  js  c++  java
  • JAVA反射机制:在泛型为Integer的ArrayList中存放一个String类型的对象、在泛型为String的ArrayList中存放一个integer类型的对象、在泛型为Map的ArrayList中存放一个integer/String/HashMap类型的对象

    1. 反射机制:在泛型为Integer的ArrayList中存放一个String类型的对象

    package test;
    
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.List;
    
    public class TestReflect1 {
        /**
         * 反射机制:在泛型为Integer的ArrayList中存放一个String类型的对象
         * @param args
         * @throws NoSuchMethodException
         * @throws SecurityException
         * @throws IllegalAccessException
         * @throws IllegalArgumentException
         * @throws InvocationTargetException
         */
        public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            List<Integer> list = new ArrayList<Integer>();
            Method method = list.getClass().getMethod("add", Object.class);
            method.invoke(list, 1);
            method.invoke(list, 2);
            method.invoke(list, "Java反射机制测试");
            method.invoke(list, 3);
            System.out.println(list);
        }
    }

    运行结果:


    2. 反射机制:在泛型为String的ArrayList中存放一个integer类型的对象

    package test;
    
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.List;
    
    public class TestReflect2 {
        /**
         * 反射机制:在泛型为String的ArrayList中存放一个integer类型的对象
         * @param args
         * @throws NoSuchMethodException
         * @throws SecurityException
         * @throws IllegalAccessException
         * @throws IllegalArgumentException
         * @throws InvocationTargetException
         */
        public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            List<String> list = new ArrayList<String>();
            Method method = list.getClass().getMethod("add", Object.class);
            method.invoke(list, "Java反射机制测试");
            method.invoke(list, "Java反射机制测试");
            method.invoke(list, 1);
            method.invoke(list, "Java反射机制测试");
            System.out.println(list);
        }
    }

    运行结果:


    3. 反射机制:在泛型为Map的ArrayList中存放一个integer/String/HashMap类型的对象

    package test;
    
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class TestReflect3 {
        /**
         * 反射机制:在泛型为Map的ArrayList中存放一个integer/String/HashMap类型的对象
         * @param args
         * @throws NoSuchMethodException
         * @throws SecurityException
         * @throws IllegalAccessException
         * @throws IllegalArgumentException
         * @throws InvocationTargetException
         */
        public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            List<Map<String, Object>> list = new ArrayList<>();
            Method method = list.getClass().getMethod("add", Object.class);
            method.invoke(list, 1);
            method.invoke(list, "Java反射机制测试");
            
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", "zhangsan");
            map.put("sex", "男");
            
            method.invoke(list, map);
            System.out.println(list);
        }
    }

    运行结果:

  • 相关阅读:
    第六章脑图
    20192419实验四 《Python程序设计》实验报告
    20192419 实验三《Python程序设计》实验报告
    20192419 实验二《Python程序设计》实验报告
    20192419 实验一《Python程序设计》实验报告
    小组讨论-第十章 密码学及应用
    2019-2020-1学期 20192419 《网络空间安全专业导论》第十二周学习总结 (读书笔记)
    2019-2020-1学期 20192419 《网络空间安全专业导论》第十二周学习总结 (读书心得)
    2019-2020-1学期 20192419 《网络空间安全专业导论》第十一周学习总结 (读书心得)
    2019-2020-1学期 20192419 《网络空间安全专业导论》第十一周学习总结 (读书笔记)
  • 原文地址:https://www.cnblogs.com/hooly/p/11230279.html
Copyright © 2011-2022 走看看