反射
概念:将类的各个组成部分封装为其他对象,这就是反射机制
Class对象功能:
获取功能:
1,获取成员变量
Field[ ] getFields():获取所有public修饰的
Field getField(String name):获取指定名称的public修饰的成员变量
Field[ ] getDeclaredFields() :获取所有的成员变量,不考虑修饰符
Field getDeclaredField(String name)
2,获取构造方法
Constructor<?>[ ] getConstructors()
Constructor getConstructor(类<?>…parameterTypes)
Constructor getDeclaredConstructor(类<?>…parameterTypes)
Constructor<?>[ ] getDeclaredConstructors()
3,获取成员方法
* Method[ ] getMethods()
* Method getMethod(String name,类<?>…parameterTypes)
* Method[ ] getDeclaredMethods()
* Method getDeclaredMethod(String name,类<?>...parameterTypes)
1
2
4,获取类名
* string getName()
Field:成员变量
操作:
1,设置值
void set(Object obj,Object value)
2,获取值
get(Object obj)
3,忽略访问权限修饰符的安全检查
setAccessible(true):暴力反射
————————————————
版权声明:本文为CSDN博主「南方的树~」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42384085/article/details/97823437
原文:https://www.cnblogs.com/xincheng1320/p/11425109.html