博客
关于我
动态创建对象执行方法
阅读量:587 次
发布时间: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/

你可能感兴趣的文章
mysql 字段类型类型
查看>>
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>
Mysql 学习总结(89)—— Mysql 库表容量统计
查看>>
mysql 实现主从复制/主从同步
查看>>
mysql 审核_审核MySQL数据库上的登录
查看>>
mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
查看>>
mysql 导入导出大文件
查看>>
MySQL 导出数据
查看>>
mysql 将null转代为0
查看>>
mysql 常用
查看>>
MySQL 常用列类型
查看>>
mysql 常用命令
查看>>