首先寫一個Person類:java
1 package lltse.base.reflectdemo; 2 3 public class Person 4 { 5 6 private String name ="張三"; 7 8 private int sex = 0; 9 10 private int age=10; 11 12 public String id ="中山大道"; 13 14 protected int num =123213213; 15 16 public String getName() { 17 return name; 18 } 19 20 21 22 public void setName(String name) { 23 this.name = name; 24 } 25 26 27 28 public int getSex() { 29 return sex; 30 } 31 32 33 34 public void setSex(int sex) { 35 this.sex = sex; 36 } 37 38 39 40 public int getAge() { 41 return age; 42 } 43 44 45 46 public void setAge(int age) { 47 this.age = age; 48 } 49 50 51 52 53 54 public Person(String name,int sex,int age) 55 { 56 this.name = name; 57 this.sex = sex; 58 this.age = age; 59 } 60 61 public Person() 62 { 63 } 64 65 /** 66 * @param args 67 */ 68 public static void main(String[] args) { 69 // TODO Auto-generated method stub 70 71 } 72 73 74 public int show(String name,int age) 75 { 76 System.out.println("showInfo:>>>:name"+name+"age:>>>"+age); 77 return age; 78 } 79 80 public static String getName(String name) 81 { 82 return name; 83 } 84 }
其次是測試反射的相關方法數組
1 package lltse.base.reflectdemo; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.Field; 5 import java.lang.reflect.InvocationTargetException; 6 import java.lang.reflect.Method; 7 8 public class ReflectTest { 9 /** 10 * @param args 11 * @throws ClassNotFoundException 12 * @throws SecurityException 13 * @throws NoSuchMethodException 14 * @throws InvocationTargetException 15 * @throws IllegalArgumentException 16 * @throws IllegalAccessException 17 * @throws InstantiationException 18 */ 19 public static void main(String[] args) throws ClassNotFoundException, 20 NoSuchMethodException, SecurityException, IllegalAccessException, 21 IllegalArgumentException, InvocationTargetException, 22 InstantiationException { 23 // 獲取Class方法一 24 // Person person = new Person(); 25 // Class personClazz1 = person.getClass(); 26 27 // 獲取Class方法二 28 Class personClazz2 = Person.class; 29 30 // 獲取Class方法三 31 Class personClazz3 = Class.forName("lltse.base.reflectdemo.Person"); 32 33 // 活動完整的包名和類名 34 System.out.println("personClazz3.getName():>>>" 35 + personClazz3.getName()); 36 37 /* 38 * 返回一個包含某些 Method 對象的數組,這些對象反映此 Class 39 * 對象所表示的類或接口(包括那些由該類或接口聲明的以及從超類和超接口繼承的那些的類或接口)的公共 member 方法。 40 */ 41 Method[] methods = personClazz3.getMethods(); 42 43 /* 44 * 返回 Method 對象的一個數組,這些對象反映此 Class 對象表示的類或接口聲明的全部方法, 45 * 包括公共、保護、默認(包)訪問和私有方法,但不包括繼承的方法。 46 */ 47 Method[] declaredMethods = personClazz3.getDeclaredMethods(); 48 49 for (int i = 0; i < methods.length; i++) { 50 System.out.println(methods[i].getName()); 51 } 52 53 // 返回一個包含某些 Constructor 對象的數組,這些對象反映此 Class 對象所表示的類的全部公共構造方法。 54 Constructor[] constructors = personClazz3.getConstructors(); 55 56 for (int t = 0; t < constructors.length; t++) { 57 System.out 58 .println("constructors[" + t + "]:>>>>" + constructors[t]); 59 } 60 61 // 實例化類對象 62 Person p = (Person) personClazz3.newInstance(); 63 // 返回一個 Method 對象,它反映此 Class 對象所表示的類或接口的指定公共成員方法。 64 Method method = personClazz3.getMethod("show", new Class[] { 65 String.class, int.class }); 66 67 // 對帶有指定參數的指定對象調用由此 Method 對象表示的底層方法。p表明被實例化的類對象 68 int returnValue = (int) method.invoke(p, "zhangsan", 30); 69 System.out.println("returnValue:>>>" + returnValue); 70 71 /* 72 * 若是底層方法是靜態的,那麼能夠忽略指定的 obj 參數。該參數能夠爲 null。 若是底層方法所需的形參數爲 0,則所提供的 args 73 * 數組長度能夠爲 0 或 null。 74 */ 75 76 Method method2 = personClazz3.getMethod("getName", 77 new Class[] { String.class }); 78 String name = (String) method2.invoke(null, "test"); 79 System.out.println("name:>>>" + name); 80 81 82 /*獲取類中自定義的私有屬性*/ 83 /* 84 * 以上代碼中,Field.setAccessible(fields, true); 是最爲關鍵的一點。 85 在使用java反射機制獲取 JavaBean 的屬性值時,若是該屬性被聲明爲private 的, 86 須要將setAccessible設置爲true. 默認的值爲false. 87 若是不設置Accessible爲true則沒法獲取私有屬性的值。 88 */ 89 Field[] fields = personClazz3.getDeclaredFields(); 90 Field.setAccessible(fields, true);// 修改訪問控制權限 91 92 for (int i = 0; i < fields.length; i++) 93 { 94 System.out.println("變量聲明類型:"+ fields[i]); 95 System.out.println("field name:>>>"+fields[i].getName() + "-> "); 96 System.out.println("field value:>>> "+fields[i].get(new Person())); 97 98 } 99 } 100 }