
本文共 2608 字,大约阅读时间需要 8 分钟。
��������������� Java ������������������������������������������������������������������������������������������������������������������������������������������������������
���������������
���������������������������������������Type������������������
Class type = MyClass.class;
������Type��������������������������������������������������������������� annotations������������������������������������
-
������������������
String className = type.getName(); // ������������
-
������������������������
boolean isInterface = type.isInterface(); // true���������������
������������������
���������������������������������������������������������������������
Method method = type.getMethod("methodName");
���������������������������������������������������������������������������������������������������������MethodNotFoundException���NoSuchMethodException���
������������
���������������������������
public class MyClass { public void sayHello() { }}
������������������������������
Object instance = new MyClass();Method method = MyClass.class.getMethod("sayHello");method.invoke(instance); // successfully
������������������������������������
Object instance = new MyClass();Method method = instance.getClass().getMethod("sayHello");method.invoke(instance); // successfully
������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������������
for (Method method : type.getMethods()) { System.out.println(method.getName());}
���������������������������
for (Method method : type.getMethods()) { try { Object result = method.invoke(type������ null); System.out.println("- " + method.getName() + "() return: " + result); } catch (IllegalAccessException e) { System.out.println("������������ " + method.getName() + "() ������"); }}
������������
-
������������������������������������������������������������������������������������������������������
AccessibleObject accessibleObject = method.getAccessible();if (accessibleObject.isAccessible()) { // ���������} else { method.setAccessible(true);}
-
��������������������������������������������������������������� method.getModifiers() ������������������������������
������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
