Java基础巩固之反射

        //全类名
        Class cls = Class.forName("cn.java_01.fanshe.Persion");
        System.out.println(cls);
        //类名.class
        Class persionClass = Persion.class;
        //System.out.println(persionClass);
        //对象.getclass
        Persion persion = new Persion();
        Class aClass = persion.getClass();
        //System.out.println(aClass);
        //结论同一个字节码文件 他在一次程序运行过程中只会被加载一次
        //System.out.println(cls==persionClass);
        //System.out.println(cls==aClass);
        //System.out.println(persionClass==aClass);
        //获取成员变量
        Field[] fields = cls.getFields();//获取所有punlic修饰的变量
        for (Field f : fields) {
            System.out.println(f);
        }
        Field[] declaredFields = cls.getDeclaredFields();//获取所有成员变量不考虑修饰符
        for (Field d:declaredFields) {
            System.out.println(d);
        }
        //忽略访问安全检查
        Field name1 = cls.getDeclaredField("name");
        name1.setAccessible(true);
        System.out.println("==============="+name1);
       // Field name2 = cls.getField("name");
       // name2.setAccessible(true);
       // System.out.println("**************"+name2);


        //获取构造方法 目的就是创建对象
        Constructor[] constructors = cls.getConstructors();
        Constructor constructor = cls.getConstructor(String.class, int.class);
        System.out.println(constructor);
        //创建对象
        Object zs = constructor.newInstance("zs", 23);
        System.out.println(zs.toString());

        Object o = cls.newInstance();//空参创建对象
        System.out.println(o.toString());
        //暴力反射
        constructor.setAccessible(true);

        //获取成员方法
        //Method method = cls.getMethod();
        System.out.println("------------------所有public方法--------------------------");
        Method[] methods = cls.getMethods();

        for (Method m :methods) {
            System.out.println(m);
            System.out.println(m.getName());
        }
        System.out.println("------------------所有public方法--------------------------");
        //获取指定方法名
        Method eat_method = cls.getMethod("eat");
        //执行方法
        Persion p = new Persion();
        eat_method.invoke(p);

        Method eat = cls.getMethod("eat", String.class);
        eat.invoke(p,"肉");


        //获取类名
        String name = cls.getName();
        System.out.println(name);
        Package aPackage = cls.getPackage();
        System.out.println(aPackage);
 public static void main(String[] args) throws Exception {
        //创建工具类对象
        //        Properties properties = new Properties();
        //        //加载配置文件 转换为集合
        //        ClassLoader classLoader = AnLi.class.getClassLoader();
        //        InputStream resourceAsStream = classLoader.getResourceAsStream("pro.properties");
        //        properties.load(resourceAsStream);
        //
        //        //获取配置文件中定义的数据
        String className = properties.getProperty("className");
        String methodName = properties.getProperty("methodName");

        //加载近内存
        Class cla = Class.forName(className);
        Object obj = cla.newInstance();

        //获取方法对象
        Method method = cla.getMethod(methodName);
        method.invoke(obj);

    }

 

你可能感兴趣的:(Java基础重新来过)