简介
反射是爪哇的一种机制,是指在运行时,能够动静获取信息,能够动静调用对象办法,例如类、办法、接口、变量等信息(无需晓得类的名字、办法名等)。
与爪哇反射相干的类如下:
类名 | 用处 |
---|---|
Class 类 |
代表类的实体,在运行的爪哇应用程序中示意类和接口 |
Field 类 |
代表类的成员变量(成员变量也称为类的属性) |
Method 类 |
代表类的办法 |
Constructor 类 |
代表类的构造方法 |
1. 获取一个将要被反射的类的办法:
1) 应用 .class
能够在编译时晓得具体类名后,获取对象
Class studentClass = Student.class;
2) 应用 Class.forName("……")
在运行时获取对象。因为会找不到类,所以要进行异样捕捉
try {Class studentClass = Class.forName("Student");
} catch(ClassNotFoundException e) {e.printStackTrace();
}
3) 应用对象的 getClass()
办法
String s = new String("Hello");
Class sClass = s.getClass();
案例
package com.github.Student
public class Test {public static void main (String[] args) {
Class studentClass = Student.class;
System.out.println(studentClass.getName());
System.out.println(studentClass.getSimpleName());
}
}
/Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=49728:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/Code/works/out/production/works com.github.Student
com.github.Student
Student
Process finished with exit code 0
2. 相干类阐明
1) Class
类
2) Field
类
3) Method
类
4) Constructor
类
I. 获取所有构造方法
public class Test {public static void main(String[] args) {
Class studentClass = Student.class;
Constructor[] constructors = studentClass.getDeclaredConstructors();
}
}
附:获取所有构造方法以及每个构造方法的参数类型
for(int i = 0; i < constructors.length; i++) {System.out.print(Modifier.toString(constructors[i].getModifiers()) + "结构参数:");
Class[] classes = constructors[i].getParameterTypes();
for(int j = 0; j < classes.length; j++) {System.out.print("[" + classes[j].getName() + "]");
}
System.out.println();}
/Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=52590:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/Code/works/out/production/works com.github.Test
public 结构参数:[java.lang.String][int]
Process finished with exit code 0
II. 获取所有 public
办法
getConstructors()
能够获取以 public
润饰的所有构造方法
public class Test {public static void main(String[] args) {
Class studentClass = Student.class;
Constructor[] constructors = studentClass.getConstructors();
}
}
III. 获取特定办法
通过向 getDelaredConstructor(……)
办法传参能够取得有着某些特定参数的办法,要进行异样捕捉因为可能找不到指定的办法
Class[] classes = {String.class, int.class};
try {
Class studentClass = Student.class;
Constructor constructor = studentClass.getConstructor(classes);
System.out.print(Modifier.toString(constructor.getModifiers()) + "结构参数:");
Class[] parameters = constructor.getParameterTypes();
for(int j = 0; j < classes.length; j++) {System.out.print("[" + classes[j].getName() + "]");
}
System.out.println();} catch(NoSuchMethodException e) {e.printStackTrace();
}
/Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=53473:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/Code/works/out/production/works com.github.Test
public 结构参数:[java.lang.String][int]
Process finished with exit code 0
3. 理论使用
I. 调用构造方法
Class[] classes = {String.class, int.class};
Class studentClass = Student.class;
Constructor constructor = studentClass.getDeclaredConstructor(classes);
// constructor.newInstance("Asetg", 10);
Student student = (Student) constructor.newInstance("Ynioh", 19);
II. 调用公有构造方法
Class[] classes = {String.class};
Class studentClass = Student.class;
Constructor constructor = studentClass.getDeclaredConstructor(classes);
constructor.setAccessible(true);
Student student = (Student) constructor.newInstance("Qwdfk", 19);
III. 调用办法
// 获取无参无返回的 show()
Method method = studentClass.getDeclaredMethod("show");
method.invoke(constructor.newInstance("Asetg", 10));
// 获取有参无返回的 repeat()
Class[] parameters = {String.class};
Method method2 = studentClass.getDeclaredMethod("repeat", parameters);
Object[] objects = {"Salute!"};
method2.invoke(constructor.newInstance("Asetg", 10), objects);
/Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=58735:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/Code/works/out/production/works com.github.Test
Student{name='Asetg', age=10}
您方才说了 Salute! 吗?
Process finished with exit code 0
IV. 获取公有字段,并批改值
Field field = studentClass.getDeclaredField("name");
field.setAccessible(true);
field.set(student, "Zxcvb");
System.out.println(field.get(student).toString());
/Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=58823:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/Code/works/out/production/works com.github.Test
Zxcvb
Process finished with exit code 0