Home >Java >javaTutorial >Detailed explanation of examples of reflection mechanism in Java
This article mainly introduces relevant information about Java reflection mechanism examples. Here is a detailed analysis of the reflection mechanism in Java. Friends in need can refer to
Java Reflection Mechanism Detailed Examples.
1. Is JAVA a dynamic language?
Generally speaking, when it comes to dynamic languages, it means that the program structure or variable type is allowed to be changed while the program is running. From this point of view, Java, like C++, is not a dynamic language.
But JAVA has a very prominent dynamic related mechanism: reflection. Through reflection, Java can load, detect and use classes that were fully summed during compilation at runtime, generate their object entities, call their methods or set values for properties. So Java is considered a semi-dynamic language.
The concept of reflection:
The reflection mechanism in Java means that in the running state, for any class, all properties and methods of this class can be known;
For any object, any of its methods can be called;
This function of dynamically obtaining information and dynamically calling object methods is called the reflection mechanism of the Java language
II , Dynamic properties
##2.1. Dynamic properties
Generate object instances during runtime;Call methods during runtime;
Change attributes during runtime
2.2. Functions that can be achieved by the Java reflection mechanism
Judge the class to which any object belongs at runtimeConstruct the class of any class at runtime Object
Judge the methods and attributes of any class at runtime
Call the method of any object at runtime
Generate a dynamic proxy
Many objects in Java programs will have two types at runtime: compile-time type and run-time type
The compile-time type is determined by the time when the object is declared. The type used is determined by the type used. The type at runtime is determined by the type actually assigned to the object.
For example:
Person p =new Student();
In addition, the program may also receive an object passed in from the outside during runtime. The compile-time type of the object is Object, but the program needs to call the method of the runtime type of the object. To solve these problems, programs need to discover real information about objects and classes at runtime. However, if it is impossible to predict which classes the object and class may belong to at compile time, and the program only relies on runtime information to discover the real information of the object and class, reflection must be used at this time
3. Java Reflection APIThe reflection API is used to generate information about classes, interfaces or objects in the current JAVA virtual machine.
Class class: The core class of reflection, which can obtain the attributes, methods and other content information of the class.
Field class: Java.lang.reflect. Represents the attributes of the class, and can get and set the attribute values of the class.Method class:Java.lang.reflect. Represents a method of a class, which can be used to obtain information about methods in a class or execute methods
Construcor class: Java.lang.reflect. Represents the constructor method of the class.
Person class
package com.pb.Reflect.classinfo; public class Person { private String name; private String gender; private int age; private Person() { // } public Person(String name, String gender, int age) { super(); this.name = name; this.gender = gender; this.age = age; } //getter、和setter方法 private String getName() { return name; } private void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString(){ return "姓名:"+name+"年龄: "+age; } }
package com.pb.Reflect.classinfo; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import javax.swing.JOptionPane; /* * 通过用户输入类的全路径,来获取该类的成员方法和属性 * Declared获取全部不管是私有和公有 * 1.获取访问类的Class对象 * 2.调用Class对象的方法返回访问类的方法和属性信息 */ public class ReflectDemo { /* * 构造方法 */ public ReflectDemo(){ //用户输入类的全路径径 //使用String组件 String classpsth=JOptionPane.showInputDialog(null,"输入类的全路径"); //使用Class.forName方法根据输入的类的全路径 返回该类的Class对象 try { Class cla = Class.forName(classpsth); //利用Class对象的cla的自审,返回方法对象集合 Method [] method=cla.getDeclaredMethods(); //返回所有的方法 System.out.println("========获取方法信息============"); for (Method meth : method) { //遍历method数组,并输出方法信息 System.out.println(meth.toString()); } System.out.println("========获取出方法信息结束============"); //获取属性利用Class对象的cla的自审,返回成员属性对象集合 Field [] field=cla.getDeclaredFields(); System.out.println("========获取成员属性信息============"); for (Field f : field) { System.out.println(f.toString()); } System.out.println("========获取成员属性信息结束============"); //获取属性利用Class对象的cla的自审,返回构造方法集合 Constructor [] constructor=cla.getDeclaredConstructors(); System.out.println("========获取成员构造方法信息============"); for (Constructor constru : constructor) { System.out.println(constru.toString()); } System.out.println("========获取成员构造方法信息结束============"); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println("路径输入错误!"); } } }
package com.pb.Reflect.classinfo; public class TestReflection { public static void main(String[] args) { ReflectDemo rd=new ReflectDemo(); } }
Result:
========获取方法信息============ public java.lang.String com.pb.Reflect.classinfo.Person.getGender() public void com.pb.Reflect.classinfo.Person.setGender(java.lang.String) public int com.pb.Reflect.classinfo.Person.getAge() public void com.pb.Reflect.classinfo.Person.setAge(int) public java.lang.String com.pb.Reflect.classinfo.Person.toString() private java.lang.String com.pb.Reflect.classinfo.Person.getName() private void com.pb.Reflect.classinfo.Person.setName(java.lang.String) ========获取出方法信息结束============ ========获取成员属性信息============ private java.lang.String com.pb.Reflect.classinfo.Person.name private java.lang.String com.pb.Reflect.classinfo.Person.gender private int com.pb.Reflect.classinfo.Person.age ========获取成员属性信息结束============ ========获取构造方法信息============ private com.pb.Reflect.classinfo.Person() public com.pb.Reflect.classinfo.Person(java.lang.String,java.lang.String,int) ========获取构造方法信息结束============
5.1. Steps
Java.lang.reflect
Get the Java.lang.Class object of the class you want to operate
Call the method of ClassUse the reflection API to operate this information
Call the getClass() method of an object
Person p = new Person(); Class cla=p.getClass();
Class cls=Person.class;
Class cla=Class.forName(“类的全路径”);
Person class. Because the object is to be declared, the constructor method public
package com.pb.Reflect.classinfo; public class Person { private String name; private String gender; private int age; public Person() { // } public Person(String name, String gender, int age) { super(); this.name = name; this.gender = gender; this.age = age; } //getter、和setter方法 private String getName() { return name; } private void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString(){ return "姓名:"+name+"年龄: "+age; } }
package com.pb.Reflect.classinfo; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import javax.swing.JOptionPane; /* * 通过用户输入类的全路径,来获取该类的成员方法和属性 * Declared获取全部不管是私有和公有 * 1.获取访问类的Class对象 * 2.调用Class对象的方法返回访问类的方法和属性信息 */ public ReflectDemo(Person p){ Class cla=p.getClass(); //利用Class对象的cla的自审,返回方法对象集合 Method [] method=cla.getDeclaredMethods(); //返回所有的方法 System.out.println("========获取方法信息============"); for (Method meth : method) { //遍历method数组,并输出方法信息 System.out.println(meth.toString()); } System.out.println("========获取出方法信息结束============"); //获取属性利用Class对象的cla的自审,返回成员属性对象集合 Field [] field=cla.getDeclaredFields(); System.out.println("========获取成员属性信息============"); for (Field f : field) { System.out.println(f.toString()); } System.out.println("========获取成员属性信息结束============"); //获取属性利用Class对象的cla的自审,返回构造方法集合 Constructor [] constructor=cla.getDeclaredConstructors(); System.out.println("========获取成员构造方法信息============"); for (Constructor constru : constructor) { System.out.println(constru.toString()); } System.out.println("========获取成员构造方法信息结束============"); } }
package com.pb.Reflect.classinfo; public class TestReflection { public static void main(String[] args) { Person p=new Person(); ReflectDemo rd=new ReflectDemo(p); } }
========获取方法信息============ public java.lang.String com.pb.Reflect.classinfo.Person.getGender() public void com.pb.Reflect.classinfo.Person.setGender(java.lang.String) public int com.pb.Reflect.classinfo.Person.getAge() public void com.pb.Reflect.classinfo.Person.setAge(int) public java.lang.String com.pb.Reflect.classinfo.Person.toString() private java.lang.String com.pb.Reflect.classinfo.Person.getName() private void com.pb.Reflect.classinfo.Person.setName(java.lang.String) ========获取出方法信息结束============ ========获取成员属性信息============ private java.lang.String com.pb.Reflect.classinfo.Person.name private java.lang.String com.pb.Reflect.classinfo.Person.gender private int com.pb.Reflect.classinfo.Person.age ========获取成员属性信息结束============ ========获取成员构造方法信息============ public com.pb.Reflect.classinfo.Person() public com.pb.Reflect.classinfo.Person(java.lang.String,java.lang.String,int) ========获取成员构造方法信息结束============
Person class is the same as above
Test class:
package com.pb.Reflect.classinfo; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; public class TestReflection { public static void main(String[] args) { /*第二种方法 Person p=new Person(); ReflectDemo rd=new ReflectDemo(p); */ /* * 第三种方式.class属性 */ Class cla=Person.class; //利用Class对象的cla的自审,返回方法对象集合 Method [] method=cla.getDeclaredMethods(); //返回所有的方法 System.out.println("========获取方法信息============"); for (Method meth : method) { //遍历method数组,并输出方法信息 System.out.println(meth.toString()); } System.out.println("========获取出方法信息结束============"); //获取属性利用Class对象的cla的自审,返回成员属性对象集合 Field [] field=cla.getDeclaredFields(); System.out.println("========获取成员属性信息============"); for (Field f : field) { System.out.println(f.toString()); } System.out.println("========获取成员属性信息结束============"); //获取属性利用Class对象的cla的自审,返回构造方法集合 Constructor [] constructor=cla.getDeclaredConstructors(); System.out.println("========获取成员构造方法信息============"); for (Constructor constru : constructor) { System.out.println(constru.toString()); } System.out.println("========获取成员构造方法信息结束============"); } }
Same as above
========获取方法信息============ public java.lang.String com.pb.Reflect.classinfo.Person.getGender() public void com.pb.Reflect.classinfo.Person.setGender(java.lang.String) public int com.pb.Reflect.classinfo.Person.getAge() public void com.pb.Reflect.classinfo.Person.setAge(int) public java.lang.String com.pb.Reflect.classinfo.Person.toString() private java.lang.String com.pb.Reflect.classinfo.Person.getName() private void com.pb.Reflect.classinfo.Person.setName(java.lang.String) ========获取出方法信息结束============ ========获取成员属性信息============ private java.lang.String com.pb.Reflect.classinfo.Person.name private java.lang.String com.pb.Reflect.classinfo.Person.gender private int com.pb.Reflect.classinfo.Person.age ========获取成员属性信息结束============ ========获取成员构造方法信息============ public com.pb.Reflect.classinfo.Person() public com.pb.Reflect.classinfo.Person(java.lang.String,java.lang.String,int) ========获取成员构造方法信息结束============
The above is the detailed content of Detailed explanation of examples of reflection mechanism in Java. For more information, please follow other related articles on the PHP Chinese website!