The specific principle of reflection is explained later, using the following code example to illustrate the application of reflection.
Write a person class first
Packageexercise01; Public classPerson {PrivateString name; Private intAge ; //Non-parametric construction method PublicPerson () {}//a method for constructing a parameter PublicPerson (String name,intAge ) { This. Name =name; This. Age =Age ; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } @Override PublicString toString () {return"Person [name=" + name + ", age=" + Age + "]"; }}
Three ways to get a class object
Class clazz1 = person. class The parameter of the new//forname method is the path to the class, that is, the package name plus the class name
There are three arrays in the class object, namely
Private Constructor[] Constructor; // constructs an array of methods that hold the constructor of the target class Private field[] field; // an array of properties that holds the target class properties Private Method[] method; // Common method arrays, common methods for storing target classes
method to get three arrays
@Test publicvoidthrows Exception { = person. Class; // get an array of constructors constructor[] Constructor = clazz.getconstructors (); // get an array of properties field[] field = clazz.getdeclaredfields (); // get an array of common methods Method[] method = clazz.getdeclaredmethods (); }
Attention:
If the class object wants to set a private property or method of the target class, add the following statement
// Get Class object Class clazz = person. class ; // get the Name property in the person class Field field = Clazz.getdeclaredfield ("name"); // the access permission for name is private, and the following statement gets the permissions, and R then the Field object can assign a value of field.setaccessible (true) to the Name property;
To invoke the non-parametric construction method of the target class
// get an argument-free construction method @Test publicvoidthrows Exception { / / Gets the Class object class Clazz = person. Class; // get an instance of person Person p = (person) cla.newinstance (); P.setname ("Example"); System.out.println (P.getname ()); }
The method of calling the target class with a parameter constructor
@Test Public voidGetConstructors ()throwsexception{Class clazz= person.class; //is passed the parameter type in the constructor method, the type is the form of class /** Here the class form of int to use Int.class, instead of Integer.class * if you want to use Integer.class, you want to change the age of the person class to an integer type */Constructor CS= Clazz.getconstructor (String.class,int.class); //setting values through a constructor with parametersperson P1 = (person) cs.newinstance ("Lila", 24); System.out.println (p1); }
Using the properties of the target class
@Test Public voidGetField () {Try{Class Clazz= Class.forName ("exercise01. Person "); //get the Name property//c2.getdeclaredfields ();//get all the propertiesField field = Clazz.getdeclaredfield ("name");
Instantiate a person object by using the person's parameterless construction method person person=(person) clazz.newinstance (); //set access permissions so that field can manipulate private propertiesField.setaccessible (true); //set the Name property of the person objectField.set (Person, "example"); System.out.println (F1.get (person)); }Catch(Exception e) {e.printstacktrace (); } }
Common methods for using target classes
@Test Public voidGetMethod ()throwsexception{Class clazz= Class.forName ("exercise01. Person "); //instantiate a Person object using the no-argument construction method of the person classPerson person =(person) clazz.newinstance (); //gets a method object that contains the SetName method of the person class /** Getdeclaredmethod Method The first parameter is the target method name of the target class, and the second parameter is the class form of the parameter type of the target method * You can write all the parameters of the target method in*/Method Method= Clazz.getdeclaredmethod ("SetName", String.class); //Set PermissionsMethod.setaccessible (true); Method.invoke (person,"Example"); //Accessing static methods//Method.invoke (NULL, "example");System.out.println (Person.getname ()); }
Here's a notable point, if you want to invoke the static method of the target class, the method of writing the Invoke method is a little different.
Because calling a static method does not have to instantiate an object, it can be called directly through the class name, so when a static method is used, the first parameter of the Invoke method is written as null, and the second parameter writes the value of the parameter of the static method, such as
Method.invoke (null, "example");
Summary of Java Reflection