1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| ======获取类====== call constructor: protected Person(String name) person.getClass() == Person.class? true Person.class == Class.forName("Person")? true ======获取构造函数====== ------1. 使用getConstructors()获取所有的public构造函数------ public reflect.Person(int,java.util.List) ------2. 使用getDeclaredConstructors()获取所有的private、protected、public构造函数------ public reflect.Person(int,java.util.List) protected reflect.Person(java.lang.String) private reflect.Person() ------3. 使用getConstructors(obj.class...)获取特定的public构造函数,并调用------ public reflect.Person(int,java.util.List) call constructor: public Person(int age, List<String> aliasNames) Person [name=null, age=9, aliasNames=[bbb]] ------4. 使用getDeclaredConstructors(obj.class...)获取特定的private、protected、public构造函数,并调用------ private reflect.Person() call constructor: private Person() Person [name=null, age=0, aliasNames=null] ======获取方法====== ------1. 使用getMethods()获取所有的public方法,包括从父类继承过来的方法------ public java.lang.String reflect.Person.toString() public void reflect.Person.setName(java.lang.String) public final void java.lang.Object.wait() throws java.lang.InterruptedException public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException public boolean java.lang.Object.equals(java.lang.Object) public native int java.lang.Object.hashCode() public final native java.lang.Class java.lang.Object.getClass() public final native void java.lang.Object.notify() public final native void java.lang.Object.notifyAll() ------2. 使用getDeclaredMethods()获取本类所有的private、protected、public方法,不包含从父类继承过来的方法------ public java.lang.String reflect.Person.toString() public void reflect.Person.setName(java.lang.String) protected java.util.List reflect.Person.addAliasName(java.lang.String) private int reflect.Person.getAge() ------3. 使用getMethod(name, obj.class...)获取特定public方法,并使用------ public void reflect.Person.setName(java.lang.String) before change name: Person [name=aaa, age=0, aliasNames=null] call method: public void setName(String name) after change name: Person [name=ccc, age=0, aliasNames=null] ------4. 使用getDeclaredMethod(name, obj.class...)获取特定的private、protected、public方法,并使用----- protected java.util.List reflect.Person.addAliasName(java.lang.String) before add alias name: Person [name=null, age=9, aliasNames=[bbb]] call method: protected List<String> addAliasName(String aliasName) after add alias name: Person [name=null, age=9, aliasNames=[bbb, ddd]] the return value is [bbb, ddd] ======获取变量====== ------1. 使用getFields()获取所有public变量------ public int reflect.Person.age ------2. 使用getDeclaredFields()获取所有private、protected、public变量------ private java.lang.String reflect.Person.name public int reflect.Person.age protected java.util.List reflect.Person.aliasNames ------3. 使用getField(fieldName)获取特定的public变量,并设置值------ public int reflect.Person.age before change age: Person [name=ccc, age=0, aliasNames=null] after change age: Person [name=ccc, age=99, aliasNames=null] ------4. 使用getDeclaredField(fieldName)获取特定的private、protected、public变量,并设置值------ protected java.util.List reflect.Person.aliasNames before change alaisNames: Person [name=null, age=9, aliasNames=[bbb, ddd]] after change alaisNames: Person [name=null, age=9, aliasNames=[ppp]]
|