Java反射机制

1 定义
在运行过程中,可以动态的调用任意一个类的属性和方法。

2 反射的好处
一般分为静态编译、动态编译两种。
静态编译:在编译时确定类型,绑定对象,即通过。
动态编译:运行时确定类型,绑定对象。动态编译最大限度发挥了java的灵活性,体现了多态的应用,用以降低类之间的藕合性。反射即这里的动态编译方式。

3 反射的主要操作
主要操作包括四个部分:获取类操作;获取属性字段操作;获取方法操作;获取构造方法操作。每一类的操作,如下:

/**
 * 反射知识点学习
 */
public class ReflectDemo {

    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException
    {
        //获取类名称
        getReflectClassName();

        //获取类方法
        getReflectClassMethodName();

        //获取类域名
        getReflectClassFieldName();

        //获取构造方法
        getReflectClassConstructFunction();
    }

    @SuppressWarnings("unchecked")
    private static void getReflectClassName() throws InstantiationException, IllegalAccessException
    {
        Class class1 = ReflectDemo.class;
        System.out.println("class1: " + class1);

        try {
            Class class2 = (Class) Class.forName("com.jesse.learn.reflect.ReflectDemo");
            System.out.println("class2: " + class2);
        }
        catch (ClassNotFoundException e) {
            System.out.println("ClassNotFoundException");
        }

        ReflectDemo reflectDemo = ReflectDemo.class.newInstance();
        Class class3 = (Class) reflectDemo.getClass();
        System.out.println("class3: " + class3);
    }

    private static void getReflectClassMethodName() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InvocationTargetException {
        Class class1 = Student.class;

        //reflecting all the declared methods of the class or interface represented by this Class object,
        //including public, protected, default (package) access, and private methods,
        //but excluding inherited methods
        for (Method method : class1.getDeclaredMethods()) {
            System.out.println("getDeclaredMethods: " + method.getName());
        }

        //reflecting all the public methods of the class or interface represented by this Class object,
        //including those declared by the class or interface
        //and those inherited from superclasses and superinterfaces
        for (Method method : class1.getMethods()) {
            System.out.println("getMethods: " + method.getName());
        }

        Student stu = class1.newInstance();
        Method method1 = class1.getMethod("setName", String.class);
        method1.invoke(stu, "wanghairong");
        System.out.println(stu.getName());

        Method method2 = class1.getMethod("getName");
        System.out.println(method2.invoke(stu));

        Method method3 = class1.getMethod("getAge");
        System.out.println(method3.invoke(stu));
    }

    private static void getReflectClassFieldName() throws NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException
    {
        Class class1 = Student.class;

        //all the accessible public fields of the class or interface represented by this Class object.
        for (Field field : class1.getFields()) {
            System.out.println("Fields: " + field.getName());
        }

        //reflecting all the fields declared by the class or interface represented by this Class object.
        //This includes public, protected, default (package) access, and private fields, but excludes inherited fields
        for (Field field : class1.getDeclaredFields()) {
            System.out.println("DeclaredFields: " + field.getName());
        }

        Field field1 = class1.getDeclaredField("name");
        Student stu = class1.newInstance();
        stu.setName("chengyingjie");

        field1.setAccessible(true);  //私有变量必须设置访问权限,否则无法直接访问
        System.out.println("name: " + field1.get(stu));
    }

    private static void getReflectClassConstructFunction() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

        Class class1 = Student.class;
        for (Constructor constructor : class1.getConstructors()) {
            System.out.println("Constructors: " + constructor.getName());
        }

        for (Constructor constructor : class1.getDeclaredConstructors()) {
            System.out.println("DeclaredConstructors: " + constructor.getName());
        }

        //注意:如果声明了带整型参数的构造函数,其参数必须声明为Integer,否则,因int不是一个包装类型,反射构建时会报错
        Constructor constructor = class1.getConstructor(String.class, Integer.class);
        System.out.println("Constructor: " + constructor.getName());

        Student stu2 = constructor.newInstance("chengwang", 20);
        System.out.println("constructorInstance: " + stu2.getName() + ":" + stu2.getAge());
    }
}

你可能感兴趣的:(Java反射机制)