The basic concept of the Java reflection mechanism and the realization of the reflection mechanism by related Class classes

1. What is reflection

Reflection can be understood as the reflection of a virtual image of a real object illuminated by a mirror back to the real object. In the program, the information of the class to which it belongs is obtained by instantiating the object.
Please add a picture description

2. Reflection

The Java reflection mechanism can dynamically obtain program information and dynamically call objects, that is, it can realize dynamic creation and dynamic compilation of objects.

1.1 In the running state
(1) can construct objects of any class
(2) can also obtain the information of the class to which any object belongs
(3) can even call any class member variables and methods
(4) of course can also obtain any Object properties and methods
Please add a picture description

3. Class class instantiation application

3.1. Three ways to instantiate Class objects

(1) Based on the fully qualified class name; Class.forName("fully qualified class name")

Class<?> c=null;
c=Class.forName("com.item.类名");//com.item:所属包名
getName()方法可以获得包括所属包名的类名
getPackage()方法可以获取所属包名

(2) Obtain according to the object: object name.getClass()

Class<?> c=null;
c=new 类名().getClass();

(3) Obtain according to the class name: class name.class

Class<?> c=null;
c=类名.class;

3.2. Two ways for the Class class to instantiate other class objects

3.2.1. Call newInstance() to instantiate through the no-argument construction method
package edu.day1;
import java.util.*;
public class Reflect {
    
    
   public static  void main(String[] args) throws InstantiationException, IllegalAccessException {
    
    
       Class<?> c=null;//?:占位符
       try {
    
    
           c=Class.forName("edu.day1.People");
       }
       catch (ClassNotFoundException e){
    
    
           e.printStackTrace();
       }
       People peo=null;
       peo=(People) c.newInstance();//实例化
       peo.setName("张小凡");
       peo.setNum(19);
       System.out.println(peo);
   }

}

class  People{
    
    
    private String name;
    private int num;

    public String getName() {
    
    
        return name;
    }

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

    public int getNum() {
    
    
        return num;
    }

    public void setNum(int num) {
    
    
        this.num = num;
    }

    public People() {
    
    
    }

    public People(String name, int num) {
    
    
        this.name = name;
        this.num = num;
    }

    @Override
    public String toString() {
    
    
        return "People{" +
                "name='" + name + '\'' +
                ", num=" + num +
                '}';
    }
}

Please add a picture description

3.2.2. Call getConstructors() to instantiate through a parameterized construction method

(1) getConstructors() obtains all the constructors in the instantiated class
(2) obtains the Constructor object corresponding to the parameterized constructor
(3) instantiates the object of the Constructor class

package edu.day1;

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

public class Book {
    
    
    public  static void main(String[] args){
    
    
        Class<?> c=null;
        try {
    
    
            c=Class.forName("edu.day1.BookReading");
        } catch (ClassNotFoundException e) {
    
    
            throw new RuntimeException(e);
        }
        BookReading book=null;
        Constructor<?> constructor[] =null;//此处获得对象数组
        constructor=c.getConstructors();//获取BookReading类的全部构造方法
        try {
    
    
            book=(BookReading) constructor[0].newInstance("碧瑶",19);//实例化BookReading对象
        } catch (InstantiationException e) {
    
    
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
    
    
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
    
    
            throw new RuntimeException(e);
        }
        System.out.println(book);


    }
}
class BookReading{
    
    
    private String bookname;
    private int index;

    public BookReading(String bookname, int index) {
    
    
        this.bookname = bookname;
        this.index = index;
    }

    public String getBookname() {
    
    
        return bookname;
    }

    public void setBookname(String bookname) {
    
    
        this.bookname = bookname;
    }

    public int getIndex() {
    
    
        return index;
    }

    public void setIndex(int index) {
    
    
        this.index = index;
    }

    @Override
    public String toString() {
    
    
        return "BookReading{" +
                "bookname='" + bookname + '\'' +
                ", index=" + index +
                '}';
    }
}

Please add a picture description

Guess you like

Origin blog.csdn.net/m0_59416550/article/details/126820999