Java關於反射的用法,Java反射用法
一. 首先是準備一個需要反射的類
1 public class Person { 2 private String name; 3 4 private int age; 5 6 public String sex; 7 8 public int tel; 9 10 private Person(String name, int age) {11 this.name = name;12 this.age = age;13 }14 15 16 public Person() {17 18 }19 20 public Person(String name, int age, String sex, int tel) {21 this.name = name;22 this.age = age;23 this.sex = sex;24 this.tel = tel;25 }26 27 public String showInfo() {28 return "name:" + name + "-age:" + age + "-sex:" + sex + "-tel" + tel;29 }30 31 private String getName() {32 33 return name;34 }35 }
二.現在開始使用反射
/** * 擷取一個類的Class對象方法 * * @param view * @throws Exception */ public void getClassMethod(View view) throws Exception { //擷取class對象有三種方法 Person person = new Person(); //第一種 Class class1 = person.getClass(); //第二種 Class class2 = Person.class; //第三種 Class class3 = Class.forName("com.adminhj.httpexercise.reflect.Person"); Log.i(TAG, "class1==class2-->" + (class1 == class2)); Log.i(TAG, "class2==class3-->" + (class2 == class3)); }
列印的結果是:
com.adminhj.httpexercise.reflect.ReflectActivity: class1==class2-->true
com.adminhj.httpexercise.reflect.ReflectActivity: class2==class3-->true
1 /** 2 * 通過反射擷取Person類的共有構造方法 3 * 4 * @param view 5 */ 6 public void getConstruct(View view) throws Exception { 7 //載入person類的class對象 8 Class class1 = Class.forName("com.adminhj.httpexercise.reflect.Person"); 9 //擷取指定的構造方法 public Person(String name, int age, String sex, int tel)10 // ,如果構造方法中沒有參數可不要寫如例如 Constructor constructor = class1.getConstructor();11 Constructor constructor = class1.getConstructor(String.class, int.class, String.class, int.class);12 //new出指定構造方法的對象13 Object obj = constructor.newInstance("張三", 22, "男", 110);14 //擷取反射類中的公用屬性15 Field sex = class1.getField("sex");16 Field tel = class1.getField("tel");17 Log.i(TAG, "sex:" + sex.get(obj) + "-------tel:" + tel.get(obj));18 //擷取反射類中指定的的公用方法19 Method showInfo = class1.getMethod("showInfo");20 //執行方法21 String info = (String) showInfo.invoke(obj);22 Log.i(TAG, "info" + "-------" + info);23 24 }
列印結果:
1 com.adminhj.httpexercise.reflect.ReflectActivity: sex:男-------tel:110
1 com.adminhj.httpexercise.reflect.ReflectActivity: info-------name:張三-age:22-sex:男-tel110
1 /** 2 * 通過反射擷取Person類的私人構造方法 3 * 4 * @param view 5 * @throws Exception 6 */ 7 public void getDeclaredConstruct(View view) throws Exception { 8 //載入person類的class對象 9 Class class1 = Class.forName("com.adminhj.httpexercise.reflect.Person");10 //擷取指定的構造方法 private Person(String name, int age)11 Constructor constructor = class1.getDeclaredConstructor(String.class, int.class);12 //設定這個私人的構造方法是可進入的,不設定則會報錯13 constructor.setAccessible(true);14 //建立執行個體對象15 Object obj = constructor.newInstance("裡斯", 2000);16 17 18 //擷取所有屬性包括私人,class1.getDeclaredField() 此方法是擷取所有屬性不包括私人19 //20 Field[] declaredFields = class1.getDeclaredFields();21 for (Field field : declaredFields) {22 field.setAccessible(true);23 24 //注釋掉的是在使用私人熟悉的時候需要注意的25 //field.setAccessible(true); 設定屬性可見26 //field.set(obj, "張飛"); 給屬性賦值27 28 Log.i(TAG, field.getName() + "-------" + field.get(obj));29 }30 //擷取反射類中指定的的私人方法31 Method getNameAndAge = class1.getDeclaredMethod("getNameAndAge");32 //設定私人方法可執行33 getNameAndAge.setAccessible(true);34 //執行方法35 String result = (String) getNameAndAge.invoke(obj);36 37 Log.i(TAG, result);38 }
列印結果:
com.adminhj.httpexercise.reflect.ReflectActivity: age-------2000com.adminhj.httpexercise.reflect.ReflectActivity: name-------裡斯com.adminhj.httpexercise.reflect.ReflectActivity: sex-------nullcom.adminhj.httpexercise.reflect.ReflectActivity: tel-------0
1 com.adminhj.httpexercise.reflect.ReflectActivity: name=裡斯----------age=2000
學無止盡 小夥伴們加油哦