2 反射
2.1 反射概述
- Java反射机制:是指在运行时去获取一个类的变量和方法信息。然后通过获取到的信息来创建对象,调用方法的一种机制。由于这种动态性,可以极大的增强程序的灵活性,程序不用在编译期就完成确定,在运行期仍然可以扩展
2.2 反射获取Class类的对象
- 我们要想通过反射去使用一个类,首先我们要获取到该类的字节码文件对象,也就是类型为Class类型的对象这里我们提供三种方式获取Cass类型的对象
-
1、使用类的class属性来获取该类对应的Class对象。举例: Student.class
将会返回Student类对应的Class对象
-
2、调用对象的getClass()
方法, 返回该对象所属类对应的Class对象
该方法是Object类中的方法,所有的Java对象都可以调用该方法
-
3、使用Class类中的静态方法forName(StringclassName)
, 该方法需要传入字符串参数,该字符串参数的值是某个类的全路径,也就是完整包名的路径
package test;
public class Demo {
public static void main(String[] args) throws ClassNotFoundException {
Class<Student> c1 = Student.class;
System.out.println(c1);
Class<Student> c2 = Student.class;
System.out.println(c1 == c2);
Student s = new Student();
Class<? extends Student> c3 = s.getClass();
System.out.println(c1 == c3);
Class<?> c4 = Class.forName("test.Student");
System.out.println(c1 == c4);
}
}
2.3 反射获取构造方法并使用
方法名 | 说明 |
---|
Constructor<?>[] getConstructors() | 返回所有公共构造方法对象的数组 |
Constructor<?> [] getDeclaredConstructors [dɪˈkleəd] | 返回所有构造方法对象的数组 |
Constructor<T> getConstructor(Class<?>...parameterTypes) | 返回单个公共构造方法对象 |
Constructor<T> getDeclaredConstructor(Class<?> ...parameterTypes) | 返回单个构造方法对象 |
方法名 | 说明 |
---|
T newInstance(Object…initargs) | 根据指定的构造方法创建对象 |
package test;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Demo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> c = Class.forName("test.Student");
Constructor<?>[] cons1 = c.getConstructors();
for(Constructor con:cons1) {
System.out.println(con);
}
System.out.println("--------");
Constructor<?>[] cons2 = c.getDeclaredConstructors();
for(Constructor con:cons2) {
System.out.println(con);
}
System.out.println("--------");
Constructor<?> con1 = c.getConstructor();
Object obj1 = con1.newInstance();
System.out.println(obj1);
}
}
- 案例1:
package test;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Demo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> c = Class.forName("test.Student");
Constructor<?> con = c.getConstructor(String.class, int.class, String.class);
Object obj = con.newInstance("小白",10, "成都");
System.out.println(obj);
}
}
- 案例2:
package test;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Demo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> c = Class.forName("test.Student");
Constructor<?> con = c.getDeclaredConstructor(String.class);
con.setAccessible(true);
Object obj = con.newInstance("小黑");
System.out.println(obj);
}
}
2.4 反射获取成员变量并使用
方法名 | 说明 |
---|
Field[ ] getFields() | 返回所有公共成员变量对象的数组 |
Field[ ] getDeclaredFields() | 返回所有成员变量对象的数组 |
Field getField(String name) | 返回单个公共成员变量对象 |
Field getDeclaredField(String name) | 返回单个成员变量对象 |
方法名 | 说明 |
---|
void set(Object obj, Object value) | 给obj对象的成员变量赋值为value |
package test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class Demo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
Class<?> c = Class.forName("test.Student");
Field[] fields1 = c.getFields();
for(Field field:fields1) {
System.out.println(field);
}
System.out.println("--------");
Field[] fields2 = c.getDeclaredFields();
for(Field field:fields2) {
System.out.println(field);
}
System.out.println("--------");
Field addressField = c.getField("address");
System.out.println(addressField);
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
addressField.set(obj,"成都");
System.out.println(obj);
}
}
- 案例:
package test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class Demo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
Class<?> c = Class.forName("test.Student");
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
Field nameField = c.getDeclaredField("name");
nameField.setAccessible(true);
nameField.set(obj,"小黑");
Field ageField = c.getDeclaredField("age");
ageField.setAccessible(true);
ageField.set(obj,10);
Field addressField = c.getDeclaredField("address");
addressField.setAccessible(true);
addressField.set(obj,"成都");
System.out.println(obj);
}
}
2.5 反射获取成员方法并使用
方法名 | 说明 |
---|
Method[ ] getMethods() | 返回所有公共成员方法对象的数组,包括继承的 |
Method[ ] getDeclaredMethods() | 返回所有成员方法对象的数组,不包括继承的 |
Method getMethod(String name, Class<?> … parameterTypes) | 返回单个公共成员方法对象 |
Method getDeclaredMethod(String name, Class<?> … parameterTypes) | 返回单个成员方法对象 |
方法名 | 说明 |
---|
Object invoke(Object obj, Objet… args) | 调用obj对象的成员方法,参数是args,返回值是Object类型 |
package test;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Demo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> c = Class.forName("test.Student");
Method[] methods1 = c.getMethods();
for(Method method:methods1) {
System.out.println(method);
}
System.out.println("---------");
Method[] methods2 = c.getDeclaredMethods();
for(Method method:methods2) {
System.out.println(method);
}
Method m = c.getMethod("method1");
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
m.invoke(obj);
}
}
-
案例:
-
Student类
package test;
public class Student {
private String name;
int age;
public String address;
public Student() {
}
private Student(String name) {
this.name = name;
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
private void function() {
System.out.println("function");
}
public void method1() {
System.out.println("method1");
}
public void method2(String s) {
System.out.println("method2:" + s);
}
public String method3(String s, int i) { return s + "," + i; }
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
package test;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Demo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> c = Class.forName("test.Student");
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
Method m1 = c.getMethod("method1");
Method m2 = c.getMethod("method2", String.class);
Method m3 = c.getMethod("method3", String.class, int.class);
Method m4 = c.getDeclaredMethod("function");
m1.invoke(obj);
m2.invoke(obj,"小黑");
Object o = m3.invoke(obj, "小白", 10);
System.out.println(o);
m4.setAccessible(true);
m4.invoke(obj);
}
}
2.6 案例
2.6.1案例1:通过反射往ArrayList<Integer>
集合中,添加字符串数据
package test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
ArrayList<Integer> array = new ArrayList<>();
Class<? extends ArrayList> c = array.getClass();
Method m = c.getMethod("add", Object.class);
m.invoke(array, "小黑");
m.invoke(array, "小黑");
m.invoke(array, "小黑");
System.out.println(array);
}
}
2.6.2 案例2:通过配置文件运行类中的方法
package test;
public class Student {
public void study() {
System.out.println("好好学习天天向上");
}
}
className=test.Student
methodName=study
package test;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
public class Demo {
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Properties prop = new Properties();
FileReader fr = new FileReader("E:\\test\\Demo\\class.txt");
prop.load(fr);
fr.close();
System.out.println(prop);
String className = prop.getProperty("className");
String methodName = prop.getProperty("methodName");
Class<?> c = Class.forName(className);
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
Method m = c.getMethod(methodName);
m.invoke(obj);
}
}