The use of reflection mechanism in java

1. JAVA reflection mechanism is in the running state

For any class, you can know all the attributes and methods of this class;

For any object, any one of its methods and properties can be called;

The function of dynamically obtaining information and dynamically calling methods of objects is called the reflection mechanism of the java language.

2. Use of reflection mechanism

Define an Animal class:

package com.phome.reflection;

public class Animal {
    public String name1;
    private int age;

    public String getName() {
        return name1;
    }

    public void setName(String name1) {
        this.name1 = name1;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // 一些方法
    public void publicMethod(String name){
        System.out.println("--------公共方法publicMethod被执行:"+name);
    }
    private void privateMethod(){
        System.out.println("--------私有的privateMethod被执行");
    }
    protected void protectedMethod(){
        System.out.println("--------受保护的protectedMethod被执行");
    }
    void method(){
        System.out.println("--------无限制类型Method被执行");
    }

    //一些构造方法
    public Animal() {
        System.out.println("--------无参数的构造方法被执行");
    }
    public Animal(String name){
        System.out.println("--------带有一个参数的构造方法被执行:"+name);

    }
    public Animal(String name,int age){
        System.out.println("--------带有两个参数的构造方法被执行:" + name +"----"+age);
    }
}

Obtain the Animal class through the reflection mechanism:

package com.phome.reflection;

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

public class ReflectionMethod {
    public static void main(String [] args) throws Exception {
        // 反射获取类方法一
        System.out.println("------------------------------1");
        Animal animal = new Animal();
        Class animalClass = animal.getClass();
        Method method1 = animalClass.getMethod("action");
        method1.invoke(animalClass.newInstance());
        // 反射获取类方法二
        System.out.println("------------------------------2");
        Class animalClass2 = Animal.class;
        Method method2 = animalClass2.getDeclaredMethod("action");
        method2.invoke(animalClass2.newInstance());
        // 反射获取类方法三(常用)
        System.out.println("------------------------------3");
        Class animalClass3 = Class.forName("com.phome.reflection.Animal");
        Method method3 = animalClass3.getMethod("action");
        method3.invoke(animalClass3.newInstance());
    }
}

Call various methods and properties in Animal through reflection mechanism:

package com.phome.reflection;

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

public class ReflectionUse {
    public static void main(String[] args) throws Exception {
        System.out.println("************1、获取类中的构造方法*************");
        Class animal = Class.forName("com.phome.reflection.Animal");
//        ReflectionUse.getAnimalConstructor(animal);
        ReflectionUse.getAnimalMethod(animal);
    }

    /**
     * 获取构造方法
     * @param className
     * @throws Exception
     */
    public static void getAnimalConstructor(Class className) throws Exception {
        System.out.println("*************获取所有构造方法开始************");
        Constructor[] cons = className.getConstructors();
        for (Constructor con: cons) {
            System.out.println(con);
        }
        System.out.println("*************获取所有构造方法结束************");

        System.out.println("*************获取并执行无参数的构造方法开始************");
        Constructor constructor = className.getConstructor(null);
        System.out.println(constructor);
        constructor.newInstance();// 执行无参构造方法
        System.out.println("*************获取无参数的构造方法结束************");

        System.out.println("*************获取并执行一个参数的构造方法开始************");
        Constructor con1 = className.getConstructor(String.class);
        System.out.println(con1);
        con1.newInstance("今天也要开心呀铁子");
        System.out.println("*************获取并执行一个参数的构造方法结束************");

        System.out.println("*************获取并执行两个参数的构造方法开始************");
        Constructor con2 = className.getConstructor(String.class,int.class);
        System.out.println(con2);
        con2.newInstance("一起嗨呀",666);
        System.out.println("*************获取并执行两个参数的构造方法结束************");
    }

    /**
     * 获取方法
     * @param className
     */
    public static void getAnimalMethod(Class className) throws Exception{
        System.out.println("*************1、获取类中的所有公共方法开始************");
        Method[] methods = className.getMethods();
        for (Method method:methods
             ) {
            System.out.println(method);
        }
        System.out.println("*************获取类中的所有公共方法结束************");

        System.out.println("*************2、获取类中的所有方法开始************");
        Method[] methods1 = className.getDeclaredMethods();
        for (Method method: methods1
             ) {
            System.out.println(method);
        }
        System.out.println("*************获取类中的所有方法结束************");

        System.out.println("*************3、执行公共的方法开始************");
        Field field = className.getField("name1");
        Object obj = className.getConstructor().newInstance();
        field.set(obj,"这是设置的参数");
        Animal animal = (Animal) obj;
        System.out.println(animal.getName());
        Method method = className.getMethod("publicMethod",String.class);
        method.invoke(className.getConstructor().newInstance(),"反射真好用");
        System.out.println("*************3、执行公共的方法结束************");

        System.out.println("*************4、执行非公共的方法开始************");
        Method method1 = className.getDeclaredMethod("privateMethod");
        method1.setAccessible(true);// 解除私有限制
        method1.invoke(className.newInstance());
        Method method2 = className.getDeclaredMethod("protectedMethod");
        method2.invoke(className.newInstance());
        Method method3 = className.getDeclaredMethod("method");
        method3.invoke(className.newInstance());
        System.out.println(method1.equals(method2));
        System.out.println(method1==method3);
        System.out.println(method2==method3);
        System.out.println("*************4、执行非公共的方法结束************");
    }
}

Guess you like

Origin blog.csdn.net/u013804636/article/details/106947488