Java -- 反射

参考https://blog.csdn.net/sinat_38259539/article/details/71799078

package com.practice.java;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

class Person {
    public int age;
    private String name;

    public Person() {

    }

    private Person(String name) {

        this.name = name;
    }

    public void fun1() {

        System.out.println("Person fun1()");
    }

    private void fun2() {

        System.out.println("Person fun2()");
    }

    public String getName() {

        return this.name;
    }
}

public class Reflect {
    //首先,必须拿到 Class对象
    public static void main(String[] args) throws
            ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //1、通过 .getClass() 获取 Class 对象
        //Person p1 = new Person();
        //Class cl1 = p1.getClass();
        //2、通过 类名 .class , 这说明任何一个类都有一个隐含的静态成员变量 class
        Class cl2 = Person.class;
        //3、通过 Class 的 forName() 静态方法来获取,用的最多,但可能抛出 ClassNotFoundException 异常
        //Class cl3 = Class.forName("Person");
        //System.out.println(cl1 == cl2);
        //System.out.println(cl2 == cl3);
        //System.out.println(cl1 == cl3);

        /**
         * 面试问题:
         * Class对象的个数?
         * 2、三个最重要的类型?
         *Field     Method     Constructor
         * newInstance;
         */
        //得到类名
        String classname = cl2.getName();
        System.out.println(classname);
        //获取类的 public 属性
        Field[] fields = cl2.getFields();
        for(Field fields1 : fields){
            System.out.println(fields1);
        }
        //获取类的所有属性  包括私有属性
        Field[] field = cl2.getDeclaredFields();
        for(Field fields1 : field){
            System.out.println(fields1);
        }
        //获得类的所有方法
        Method[] methonds = cl2.getDeclaredMethods();
        for(Method methond : methonds){
            System.out.println(methond.getName());
        }
        //获取指定的属性public
        Field f1 = cl2.getField("age");
        System.out.println(f1);
        //获取private私有属性
        Field f2 = cl2.getDeclaredField("name");//抛出异常
        System.out.println(f2);
        //启用和禁用访问安全检查的开关,值为 true,
        //则表示反射的对象在使用时应该取消 java 语言的访问检查;反之不取消
        f2.setAccessible(true);
        System.out.println("====newInstance====");
        Person p2 = (Person)(cl2.newInstance());//抛出异常
        Person p3 = (Person)(cl2.newInstance());
        f2.set(p2,"哈哈");
        System.out.println(f2.get(p2));
        System.out.println("=======无参数的私有构造函数====");
        Constructor c1=cl2.getDeclaredConstructor();
        c1.setAccessible(true);
        Person p1=(Person)c1.newInstance();
        System.out.println("无参数的私有构造函数\t"+p1);
        //得到带有一个参数的私有构造函数
        Constructor c2 = cl2.getDeclaredConstructor(String.class);
        c2.setAccessible(true);
        Person person = (Person)c2.newInstance("baishaorong");
        System.out.println("有参数的构造函数\t"+person.getName());
    }
}

打印结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/xyxy66/article/details/84067290