https://blog.csdn.net/jiahao1186/article/details/81699582
In reflection, to obtain a Class or call a method of a Class, first obtain the Class object of the Class.
1. Get Class object
In the Java API, there are three methods to obtain Class objects:
First, use the Class.forName static method.
Premise: the full pathname of the class has been specified.
Second, use the. class method.
Note: only applicable to classes that have been explicitly operated before Compilation
Third, use the getClass() method of the class object.
Suitable when there are object examples
Code example:
package com.reflection; /** * Created by Liuxd on 2018-08-15. */ public class User { private String name; private Integer age; public User() { } public User(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
package com.reflection; /** * Created by Liuxd on 2018-08-15. */ public class TestReflection { public static void main(String[] args) { // First, Class.forName Class clazz1 = null; try { clazz1 = Class.forName("com.reflection.User"); } catch (ClassNotFoundException e) { e.printStackTrace(); } // Second, get the object through the object instance method Class clazz2 = User.class; // Third, through the getClass method of the Object class User user = new User(); Class clazz3 = user.getClass(); System.out.println(clazz1); System.out.println(clazz2); System.out.println(clazz3); } }
2. Get object instance
There are two methods:
2.1. Obtain the corresponding instance directly with bytecode file
// Call the parameterless constructor. If not, an exception will be reported. Object o = clazz.newInstance();
2.2 for a class with a constructor with parameters, first obtain its construction object, and then obtain an instance through the construction method class:
/ /Get object of constructor class Constroctor constroctor = clazz.getConstructor(String.class,Integer.class); / // Initializing an object using the newInstance method of the constructor object Object obj = constroctor.newInstance("Long Ge", 29);
Code example:
package com.reflection; import java.lang.reflect.Constructor; /** * Created by Liuxd on 2018-08-15. */ public class TestReflection { public static void main(String[] args) { // First, Class.forName Class clazz1 = null; try { clazz1 = Class.forName("com.reflection.User"); } catch (ClassNotFoundException e) { e.printStackTrace(); } // Second, get the object through the object instance method Class clazz2 = User.class; // Third, through the getClass method of the Object class User user = new User(); Class clazz3 = user.getClass(); System.out.println(clazz1); System.out.println(clazz2); System.out.println(clazz3); User user1 = null; try { user1 =(User) clazz1.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } user1.setName("Terminator"); user1.setAge(1500); System.out.println("user1:"+user1.toString()); User user2 = null; try { // Get constructor Constructor constroctor = clazz2.getConstructor(String.class,Integer.class); // Initializing an object through the newInstance method of the constructor object user2 = (User) constroctor.newInstance("Long Ge",29); } catch (Exception e) { e.printStackTrace(); } System.out.println("user2:"+user2.toString()); } }