反射学习

反射

  1. 一个类正常被执行的流程

.java->.class->jvm-运行期间系统->操作系统->物理硬件

2.反射演变

 

 

 

 

 

3.java中反射的常用API

  Class

属性 Field

方法 Method

构造器 Constructor

 

如何判断一个类是属于JDK本身类还是属于自定义类?

通过类加载器判断

Integer.class.getClassLoader();    null

Student.class.getClassLoader();   sun.misc.Launcher$AppClassLoader@6d06d9c

 

获取Class

Student.class;

New student.getClass();

Class.forName(“com.demo.Student”);

Student.class.getClassLoader.loadClass(“com.demo.Student”);

操作Class对象中的非public字段,public字段

Class clazz=student.class;

获取所有public字段

Field[] field=clazz.getFields(); 实现接口的属性也可以拿到

获取本类中所有定义字段

Fiedls=clazz.getDeclaredFields();

操作某个具体名称字段

Field field=clazz.getDeclaredField(“stuGender”)

field.setAccessible (true);//暴力

Object instance=clazz.newInstance ();

field.set(instance,"");

//属性描述器

PropertyDescriptor pd=new PropertyDescriptor (field.getName (),clazz);

Method writeMethod=pd.getWriteMethod ();

Method readMethod=pd.getReadMethod ();

writeMethod.invoke (instance,"ge");

Object reVal=readMethod.invoke (instance);

类的元信息

类名称、全名称、访问修饰符

属性:名称、类型、访问修饰符

方法:方法名称、方法返回类型、方法参数列表、方法参数个数、访问修饰符

构造函数:访问修饰符、构造函数名称

Java中的几种访问修饰符?

Private/protected/默认的(default/public-->反射通过权重算法来判断访问修饰符

Modifier.toString (clazz.getModifiers ());//获取访问修饰符

转载于:https://www.cnblogs.com/czzxjsj/p/9809345.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值