
本文共 5169 字,大约阅读时间需要 17 分钟。
Reflection���Java������������������������������������������������������������������������������������������������������������������������������Reflection������������������������������������������������������������������������������������������ internals ������������
1. ������Class���������������
���������������������������������������Class���������Java���������������������������������
- Class.forName("���������")���������������������������������������������Class������������������������������������������������������������������������������
- ������.class���������������������class������Class������������������������������������������Class���������
- ������.getClass()������������������getClass()���������������������������������Class���������
���������������������������������������������������������������������������������������������������������������������������������Class���������ventional������������Class���������
2. Class���������������������
Class���������Reflection������������������������������������������������������������������
- ���������������������
- ������public������������������������Field[] getFields()
- ���������������������public���������������Field getField(String name)
- ���������������������������������������������������Field[] getDeclaredFields()
- ������������������������������������������Field getDeclaredField(String name)
- ���������������������
- ������public������������������������Constructor[] getConstructors()
- ������������������������������������������Constructor getConstructor(Class<?>... parameterTypes)
- ������������������������������������Constructor declaredConstructor = con.getDeclaredConstructor(...)
- ������������������������������������Constructor con = con.getDeclaredConstructor(int.class, String.class); con.newInstance(new Object[]{"1", "������"});
- ���������������������
- ������public������������������������Method[] getMethods()
- ������������������������������������Method getMethod(String name)
- ���������������������������������������������������Method[] getDeclaredMethods()
- ������������������������������������Method getDeclaredMethod(String name)
���������Class���������������������������������������������getName()
3. Field���������
Field���������������������������������������������������������������Field���������������������������
- ������������Object obj = field.get(obj)
- ������������field.set(obj, value)
- ���������������������������������������������field.setAccessible(true); ���������������������������private���protected���������������������
4. Constructor���������
Constructor������������������������������������������������������������������Constructor newInstance(Object...)������������������������������������������public������������������������������newInstance���������������������������������������������������������������������������������������������������setAccessible(true)������������������������
5. Method���������
Method���������������������������������������������������������������������Method.invoke()������������������������������������������������������������������������private���������������������setAccessible(true)���������������������
������������
���������������������������������������������������������
public class Person { private String name; private int age; public String a; protected String b; private String c; private String d; public Person(String name, int age) { this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public int getAge() { return age; } public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", a='" + a + '\'' + ", b='" + b + '\'' + ", c='" + c + '\'' + ", d='" + d + '\'' + '}';}
public class ReflectDemo { public static void main(String[] args) throws Exception { Class personClass = Person.class; // ������������������ Field a = personClass.getDeclaredField("a"); a.setAccessible(true); Person p = new Person(); a.set(p, "������"); System.out.println(p); // Person{a='������', ...} System.out.println("----------"); // ������������������ Constructor con = personClass.getConstructor(String.class, int.class); con.newInstance("������", 23); System.out.println(new Object(con.newInstance("������", 24))); System.out.println("----------"); // ������������������ Method eatMethod = personClass.getMethod("eat"); eatMethod.invoke(p); // ������eat... }}
���������������������������������������������������������������������������������������������������������������
���������������������������������������������������Java������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
