博客
关于我
动态创建对象执行方法
阅读量:581 次
发布时间:2019-03-11

本文共 2556 字,大约阅读时间需要 8 分钟。

Java 反射编程指南:对象创建与方法调用

1. 动态创建对象

通过反射,可以在运行时动态创建类对象。以下是常用的实现方式:

使用 newInstance 方法

调用类对象的 newInstance 方法,可以Create对象:

User user = (User) c1.newInstance();

使用DeclaredConstructor 创建对象

如果需要指定构造器参数,可以使用反射API获取指定构造器,并用 newInstance 创建对象:

Constructor con = c1.getDeclaredConstructor(int.class, String.class, int.class);User user = (User) con.newInstance(1, "XXX", 18);

2. 调用指定方法

获取方法

通过反射获取目标方法,然后用 invoke 调用:

Method setName = c1.getDeclaredMethod("setName", String.class);setName.invoke(u, "XXXXX");

关闭访问检测

如果方法为私有或受访问限制,可以用 setAccessible(true) 激活:

Method getName = c1.getDeclaredMethod("getName");getName.setAccessible(true);long startTime = System.currentTimeMillis();for (int i = 0; i < 1000000000; i++) {    getDate.invoke(u); }long endTime = System.currentTimeMillis();System.out.println("花费时间:" + (endTime - startTime) + "ms");

3. managing Private Properties

设置可访问性

私有属性需要提前设置为可访问:

Field name = c1.getDeclaredField("name");name.setAccessible(true);name.set(u21, "AAAAA");

4. setAccessible 方法

操作方法

Method 和 Field 对象都有 setAccessible 方法,其作用是启用或禁用 Java 的访问检查。

启用访问检查

如果需要频繁调用反射方法,可以启用:

booleanáfico = true;_name.setAccessible(afectado);invoke方法...

禁用访问检查

如果需要保持正常检测:

boolean afectado = false;_name.setAccessible(afectado);invoke方法...

5. API 文档

方法和 Field sl Parameters

setÁc´cеся 布尔值 启用或禁用访问检测

6. 实例比较

Normal Method Invocation

public static void test1() {    User u = new User();    long startTime = System.currentTimeMillis();    for (int i = 0; i < 1000000000; i++) {        u.getName();     }    long endTime = System.currentTimeMillis();    System.out.println("耗时:" + (endTime - startTime) + "ms");}

Reflection Method Invocation

public static void test2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {    User u = new User();    Method getName = u.getClass().getDeclaredMethod("getName");    long startTime = System.currentTimeMillis();    for (int i = 0; i < 1000000000; i++) {        getName.invoke(u);     }    long endTime = System.currentTimeMillis();    System.out.println("耗时:" + (endTime - startTime) + "ms");}

Reflection Method Invocation with Access Control

public static void test3() throws NoSuchMethodException,InvocationTargetException,IllegalAccessException {    User u = new User();    Method getName = u.getClass().getDeclaredMethod("getName");    getName.setAccessible(true);    long startTime = System.currentTimeMillis();    for (int i = 0; i < 1000000000; i++) {        getName.invoke(u);     }    long endTime = System.currentTimeMillis();    System.out.println("耗时:" + (endTime - startTime) + "ms");}

转载地址:http://oxntz.baihongyu.com/

你可能感兴趣的文章
1965 - 2019 年最流行的编程语言变化
查看>>
链上钱包的博彩雷区
查看>>
GRUB2
查看>>
解决RHEL6 vncserver 启动 could not open default font 'fixed'错误.
查看>>
微信JS-SDK DEMO页面和示例代码
查看>>
Chrome查找发请求的js之黑箱调试
查看>>
CMCC登录参数分析
查看>>
GridView的另外一种分页方式,可提高加载速度
查看>>
GridView自定义删除操作
查看>>
http常见响应状态码
查看>>
Nginx Location
查看>>
解决github Git clone 慢的问题
查看>>
一张图搞定RPC框架核心原理
查看>>
Scala中的包
查看>>
参加阿里的Java面试经验
查看>>
Python微信公众号
查看>>
他来了他来了,他带着云栖大会的免费门票走来了
查看>>
Oracle笔记
查看>>
如何复用外部shell脚本
查看>>
JAVA集合类Collection浅析
查看>>