1.6反射
1.概念
反射就是在运行状态下,对于任意一个类都能够获取到它的所有属性和方法,对于任意一个对象都能够调用它的所有属性和方法。反射其实就是将一个类中的信息映射成一个个java对象
2.原理图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ut2Tc8NQ-1686273532291)(C:\Users\10059\AppData\Roaming\Typora\typora-user-images\image-20220117090447601.png)]
3.API
(1)Class对象
package learn01;
/**
* @author zhangshiqin
* @date 2022/1/17 - 9:12
* @description 获取Class对象的三种方式
*/
public class Reflection {
public static void main(String[] args) throws ClassNotFoundException {
//1.类名.class
Class<Reflection> Clazz = Reflection.class;
//2.对象.getClass()
Reflection reflection = new Reflection();
Class<? extends Reflection> reflectionClass = reflection.getClass();
//3.Class.forName("全类名")
Class<?> forNameClazz = Class.forName("learn01.Reflection");
}
}
(2)Constructor对象
public class Reflection {
private String field;
public Reflection(){}
public Reflection(String field) {
this.field = field;
}
private Reflection(String... args){
}
public static void main(String[] args)
throws ClassNotFoundException, NoSuchMethodException
, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<Reflection> Clazz = Reflection.class;
//2.1根据参数列表获取public构造器
Constructor<Reflection> constructor = Clazz.getConstructor();
//2.2根据参数列表获取构造器
Constructor<Reflection> declaredConstructor = Clazz.getDeclaredConstructor(String.class);
//2.3使用构造器new对象
Reflection instance = constructor.newInstance();
}
}
(3)Method对象
package learn01;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author zhangshiqin
* @date 2022/1/17 - 9:12
*/
public class Reflection {
public void method(){}
public void method(String args){}
private void method(Integer args){}
public static void main(String[] args)
throws ClassNotFoundException, NoSuchMethodException
, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<Reflection> Clazz = Reflection.class;
//3.1获取所有方法
Method[] methods = Clazz.getMethods();
Method[] declaredMethods = Clazz.getDeclaredMethods();
//3.2根据方法名和参数列表获取方法,如果是空参数列表的方法,那么参数列表可以传null或者不传
Method method = Clazz.getMethod("method", String.class);
Method declaredMethod = Clazz.getDeclaredMethod("method", Integer.class);
}
}
(4)Field对象
package learn01;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author zhangshiqin
* @date 2022/1/17 - 9:12
*/
public class Reflection {
private String field;
public static void main(String[] args)
throws ClassNotFoundException, NoSuchMethodException
, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
Class<Reflection> Clazz = Reflection.class;
//4.1获取所有属性
Field[] fields = Clazz.getFields();
Field[] declaredFields = Clazz.getDeclaredFields();
//4.2根据属性名获取属性对象
Field field = Clazz.getField("field");//报错,因为这只能拿public
Field declaredField = Clazz.getDeclaredField("field");
//4.3设置值
Reflection r = new Reflection();
declaredField.set(r,'abc');
//4.4获取值
String value = declaredField.get(r);
}
}
eflection r = new Reflection();
declaredField.set(r,‘abc’);
//4.4获取值
String value = declaredField.get®;
}
}