Foreword: let's talk about reflection
Codeword is not easy. Pay attention
Reprint, please explain!
Mind map:
catalogue
The following content will be explained around the Student class
Reflection read / write properties
What is reflection
In the running state, for any class, you can know all the properties and methods of the class, and for any object, you can call any of its methods and properties. This function of dynamically obtaining information and dynamically calling the methods of the object is called the reflection mechanism of java language
jdk inside jre translation: Yes Convert java files to class's file} reflection will be class file to Java file
The following content will be explained around the Student class
package com.hpw.reflect; public class Student { private String sid; private String sname; public Integer age; static { System.out.println("Load in jvm Yes!"); } public Student() { super(); System.out.println("A student object is created by calling the parameterless constructor method"); } public Student(String sid) { super(); this.sid = sid; System.out.println("Calling the constructor with one parameter creates a student object"); } public Student(String sid, String sname) { super(); this.sid = sid; this.sname = sname; System.out.println("Calling the constructor with two parameters creates a student object"); } @SuppressWarnings("unused") private Student(Integer age) { System.out.println("call Student Class private constructor to create a student object"); this.age = age; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public void hello() { System.out.println("Hello! I am" + this.sname); } public void hello(String name) { System.out.println(name + "Hello! I am" + this.sname); } @SuppressWarnings("unused") private Integer add(Integer a, Integer b) { return new Integer(a.intValue() + b.intValue()); } }
Class class
Three ways to get class objects
1.Class.forName
2. Class Class (general query)
3. Class instance of class object getClass (general addition, deletion and modification)
The code is as follows:
package com.hpw.reflect; /** * Three ways to get class objects * 1.Class.forName * 2.Class Class * 3.Class instance of a class object getClass * @author zjjt * */ public class Dome1 { public static void main(String[] args) throws Exception { Class stuClz1 = Class.forName("com.hpw.reflect.Student");//Full pathname //General query Class stuClz2 = Student.class; //General addition, deletion and modification Class stuClz3 = new Student().getClass(); } }
Reflection instantiation
The code is as follows:
package com.hpw.reflect; import java.lang.reflect.Constructor; /** * Reflection instantiation * @author zjjt * */ public class Dome2 { public static void main(String[] args) throws Exception { /** * Reflection tone parameterless constructor newInstance */ Class<Student> stuClz1 = (Class<Student>) Class.forName("com.hpw.reflect.Student"); //DemoServlet demoClz1 = Class.forName("com.hpw.DemoServlet"); Student stu1 = (Student) stuClz1.newInstance(); //DemoServlet dome1 = (DemoServlet) stuClz1.newInstance(); stu1.hello(); //demo.dopost(req,resp); /** * Reflection modulated parameter constructor * getDeclaredConstructor Difference from getConstructor * getDeclaredConstructor Private and public constructors can be obtained * getConstructor Public constructors can be obtained */ Constructor<Student> c1 = stuClz1.getConstructor(String.class); Student s1 = c1.newInstance("s001"); Constructor<Student> c2 = stuClz1.getConstructor(String.class,String.class); Student s2 = c2.newInstance("s001","Zhang San"); Constructor<Student> c3 = stuClz1.getConstructor(Integer.class); //Because it is a private constructor, open its access rights c3.setAccessible(true); Student s3 = c3.newInstance(26); } }
The output results are as follows:
Load into jvm!
A student object is created by calling the parameterless constructor method
Hello! I'm null
Calling the constructor with one parameter creates a student object
Calling the constructor with two parameters creates a student object
Call the private constructor of the Student class to create a Student object
Reflect dynamic method calls
The code is as follows:
package com.hpw.reflect; import java.lang.reflect.Method; /** * Reflect dynamic method calls * @author zjjt * */ public class Dome3 { public static void main(String[] args) throws Exception { Class<Student> stuClz1 = (Class<Student>) Class.forName("com.hpw.reflect.Student"); Student s = stuClz1.newInstance(); Method m1 = stuClz1.getDeclaredMethod("hello"); //invoke parameter 1: instantiation refers to the Student class instance //Parameter 2: the parameter value passed by the currently called method //The return value of invoke is the return value of the method called by reflection. If the called method is void, null is returned /** * Parameter free method */ System.out.println(m1.invoke(s)+"------"); /** * Parameter adjustment method */ Method m2 = stuClz1.getDeclaredMethod("hello", String.class); System.out.println(m2.invoke(s, "Zhang San")+"-----"); Method m3 = stuClz1.getDeclaredMethod("add", Integer.class,Integer.class); //Private method open access m3.setAccessible(true); System.out.println(m3.invoke(s, 10,5)); } }
The output results are as follows:
Load into jvm!
A student object is created by calling the parameterless constructor method
Hello! I'm null
null------
Hello, Zhang San! I'm null
null-----
15
Reflection read / write properties
The code is as follows:
package com.hpw.reflect; import java.lang.reflect.Field; /** * Reflection read / write properties * * @author zjjt * */ public class Dome4 { public static void main(String[] args) throws Exception { Class<Student> stuClz1 = (Class<Student>) Class.forName("com.hpw.reflect.Student"); Student s = stuClz1.newInstance(); //Get property object Field f = stuClz1.getDeclaredField("sname"); f.setAccessible(true); //Reflection write attribute f.set(s, "Zhang San"); //Read properties through OOP System.out.println(s.getSname()+"---"); //Reflection read attribute System.out.println(f.get(s)+"---"); /** * Compare how OOP and reflection read attribute values */ Student st = new Student("s002","Li Si"); st.age = 22; //OOP read System.out.println(st.getSid()+"---"); System.out.println(st.getSname()+"---"); System.out.println(st.age+"---------------------"); //Reflection gets all attribute values of the object Field[] f1 = stuClz1.getDeclaredFields(); for (Field ff : f1) { ff.setAccessible(true); System.out.println(ff.getName()+":"+ff.get(st)); } } }
The input results are as follows:
Load into jvm!
A student object is created by calling the parameterless constructor method
Zhang San---
Zhang San---
Calling the constructor with two parameters creates a student object
s002---
Li Si---
22---------------------
sid:s002
sname: Li Si
age:22
IT's over here. I'm still the primary school student studying IT
You are welcome to give advice