博客
关于我
动态创建对象执行方法
阅读量: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 索引问题集锦
查看>>
Mysql 纵表转换为横表
查看>>
mysql 编译安装 window篇
查看>>
mysql 网络目录_联机目录数据库
查看>>
MySQL 聚簇索引&&二级索引&&辅助索引
查看>>
Mysql 脏页 脏读 脏数据
查看>>
mysql 自增id和UUID做主键性能分析,及最优方案
查看>>
Mysql 自定义函数
查看>>
mysql 行转列 列转行
查看>>
Mysql 表分区
查看>>
mysql 表的操作
查看>>
mysql 视图,视图更新删除
查看>>
MySQL 触发器
查看>>
mysql 让所有IP访问数据库
查看>>
mysql 记录的增删改查
查看>>
MySQL 设置数据库的隔离级别
查看>>