
动态创建对象执行方法
发布日期:2021-05-14 16:20:27
浏览次数:12
分类:精选文章
本文共 3146 字,大约阅读时间需要 10 分钟。
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
setA��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");}
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2025年04月26日 00时02分54秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
expect 模拟交互 ftp 上传文件到指定目录下
2021-05-10
linux系统下双屏显示
2021-05-10
PDF.js —— vue项目中使用pdf.js显示pdf文件(流)
2021-05-10
我用wxPython搭建GUI量化系统之最小架构的运行
2021-05-10
我用wxPython搭建GUI量化系统之多只股票走势对比界面
2021-05-10
我用wxPython搭建GUI量化系统之财务选股工具添加日历和排序
2021-05-10
selenium+python之切换窗口
2021-05-10
重载和重写的区别:
2021-05-10
搭建Vue项目步骤
2021-05-10
账号转账演示事务
2021-05-10
idea创建工程时错误提醒的是architectCatalog=internal
2021-05-10
SpringBoot找不到@EnableRety注解
2021-05-10
简易计算器案例
2021-05-10
在Vue中使用样式——使用内联样式
2021-05-10
Explore Optimization
2021-05-10
Kali Linux 内网渗透教程 - ARP欺骗攻击 | 超详细
2021-05-10
2020Java程序设计基础(华东交通大学)章节测试免费满分答案
2021-05-10
小程序之wx:request(转)
2021-05-10