zoukankan      html  css  js  c++  java
  • 泛型擦除和反射配置文件

    一、泛型擦除

      其实程序编译后产生的.class文件中是没有泛型约束的,这种现象我们称为泛型的擦除。

      那么,我们可以通过反射技术,来完成向有泛型约束的集合中,添加任意类型的元素

    public class ReflectTest {
        public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            ArrayList<Integer> list = new ArrayList<Integer>();
            //添加元素到集合
            list.add(new Integer(30));
            list.add(new Integer("12345"));
            list.add(123);
            //list.add("哈哈");//因为有泛型类型的约束
            System.out.println(list);
            
            //通过反射技术,实现添加任意类型的元素
            //1, 获取字节码文件对象
            //Class c = list.getClass();
            //Class c = ArrayList.class;
            Class c = Class.forName("java.util.ArrayList");
            
            //2, 找到add()方法
            // public boolean add(E e)
            Method addMethod = c.getMethod("add", Object.class);
            
            //3,  执行add()方法
            addMethod.invoke(list, "哈哈");// list.add("哈哈");
            System.out.println(list);
        }

    二、反射配置文件

      通过反射配置文件,运行配置文件中指定类的对应方法

      读取Peoperties.txt文件中的数据,通过反射技术,来完成Person对象的创建

    Peoperties.txt文件内容如下:

    className=cn.oracle_01_Reflect.Person
    methodName=method5

    读取配置文件,调用指定类中的对应方法

    public class ReflectTest2 {
        public static void main(String[] args)
                throws FileNotFoundException, IOException, ClassNotFoundException, NoSuchMethodException, SecurityException,
                InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
            // 通过Properties集合从文件中读取数据
            Properties prop = new Properties();
            // 读取文件中的数据到集合中
            prop.load(new FileInputStream("properties.txt"));
            // 获取键所对应的值
            String className = prop.getProperty("className");
            System.out.println(className);
    
            // 1,获取Person.class 字节码文件对象
            Class c = Class.forName(className);
            // 2,获取构造方法
            // public Person(String name, int age, String address)
            Constructor con = c.getConstructor(String.class, int.class, String.class);
    
            // 3,创建对象
            Object obj = con.newInstance("小明", 20, "中国");
            System.out.println(obj);
    
            // 4,获取指定的方法
            // private void method5(){}
            String methodName = prop.getProperty("methodName");
            Method m5 = c.getDeclaredMethod(methodName, null);
            // 5,开启暴力访问
            m5.setAccessible(true);
            // 6,执行找到的方法
            m5.invoke(obj, null);
        }
    }
  • 相关阅读:
    POJ 3904 Sky Code (容斥原理)
    HDU 4334 Trouble (暴力)
    UVA 10325 The Lottery( 容斥原理)
    HDU 2841 Visible Trees 数论+容斥原理
    UVA11806-Cheerleaders(容斥原理+二进制)
    HDU1695 GCD (欧拉函数+容斥原理)
    HDU 5651 xiaoxin juju needs help (组合数)
    最大子矩阵和 51Nod 1051 模板题
    最大子段和 模板题 51Nod 1049
    51Nod 1006 最长公共子序列Lcs问题 模板题
  • 原文地址:https://www.cnblogs.com/luzhijin/p/13473471.html
Copyright © 2011-2022 走看看