动态创建对象执行方法
发布日期: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秒