博客
关于我
动态创建对象执行方法
阅读量: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_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>
mysql丢失更新问题
查看>>
MySQL两千万数据优化&迁移
查看>>