反射中常用的几个函数

来源:互联网 发布:win10电脑优化 编辑:程序博客网 时间:2024/04/28 07:16

反射中常用的几个函数

通过反射获取构造方法并使用:

获取构造方法

                   getConstructors

                   getDeclaredConstructors

创建对象

                   newInstance()

                   con.newInstance(“zhangsan",20);

通过反射获取成员变量并使用:

获取所有成员

                   getFields,getDeclaredFields

获取单个成员

                   getField,getDeclaredField

修改成员的值

                   set(Objectobj,Object value)
   将指定对象变量上此 Field 对象表示的字段设置为指定的新值。

通过反射获取成员方法并使用:

获取所有方法

                   getMethods

                   getDeclaredMethods

获取单个方法

                   getMethod

                   getDeclaredMethod

暴力访问

                   method.setAccessible(true);

 

0 0