博客
关于我
动态创建对象执行方法
阅读量: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中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
查看>>
mysql中的字段如何选择合适的数据类型呢?
查看>>
MySQL中的字符集陷阱:为何避免使用UTF-8
查看>>
mysql中的数据导入与导出
查看>>
MySQL中的时间函数
查看>>
mysql中的约束
查看>>
MySQL中的表是什么?
查看>>
mysql中穿件函数时候delimiter的用法
查看>>
Mysql中索引的分类、增删改查与存储引擎对应关系
查看>>
Mysql中索引的最左前缀原则图文剖析(全)
查看>>
MySql中给视图添加注释怎么添加_默认不支持_可以这样取巧---MySql工作笔记002
查看>>
Mysql中获取所有表名以及表名带时间字符串使用BetweenAnd筛选区间范围
查看>>
Mysql中视图的使用以及常见运算符的使用示例和优先级
查看>>
Mysql中触发器的使用示例
查看>>
Mysql中设置只允许指定ip能连接访问(可视化工具的方式)
查看>>
mysql中还有窗口函数?这是什么东西?
查看>>
mysql中间件
查看>>