標籤:private rate 成員類 預設 target this post generated src
反射
反射是將類抽象為一個Class對象。將類看成對象,分析它的構造方法,成員變數,方法以及內部類。
對類的分析,是將類抽象為Class對象;對構造方法的分析,是將構造方法抽象為Constructor類的對象;對成員變數的分析,是將變數抽象為Feild類的對象;對方法的分析,是將方法抽象為Method類的對象。
舉個例子:
1 public class Student { 2 public String name;//定義學生姓名 3 private int age;//定義學生年齡 4 private String sex;//定義學生性別 5 //獲得學生姓名的方法 6 public String getName() { 7 return name; 8 } 9 //學生姓名賦值的方法10 public void setName(String name) {11 this.name = name;12 }13 //獲得學生年齡的方法14 public int getAge() {15 return age;16 }17 //注意,這裡沒有為學生年齡賦值的方法,學生年齡為私人類型。18 19 //獲得學生性別的方法20 public String getSex() {21 return sex;22 }23 //學生性別賦值的方法24 public void setSex(String sex) {25 this.sex = sex;26 }27 }
1 public class Test { 2 public static void main(String[] args) { 3 Class<Student> clazz =Student.class; 4 try { 5 /Class Field Method的使用。 6 7 //獲得Student類的name屬性對象。 8 Field field1 = clazz.getDeclaredField("name"); 9 //返回name欄位的修飾符10 System.out.println(field1.getModifiers());11 //返回name欄位的類型12 System.out.println(field1.getType());13 //返回name欄位的名稱14 System.out.println(field1.getName());15 //獲得Student類的所有屬性對象。16 Field[] field2 = clazz.getDeclaredFields();17 //遍曆所有屬性18 for (Field field : field2) {19 System.out.println(field.getName());20 System.out.println(field.getType());21 System.out.println(field.getModifiers()); 22 }23 24 //為學生對象zhangsan沒有set方法的私人類型age賦值25 Student zhangsan = new Student();26 //獲得age屬性的對象27 Field age = clazz.getDeclaredField("age");28 //取消對age屬性的修飾符的檢查訪問,以便為age屬性賦值29 age.setAccessible(true);30 //為age屬性賦值31 age.set(zhangsan, 18);32 //回複對age屬性的修飾符的檢查訪問33 age.setAccessible(false);34 System.out.println(zhangsan.getAge());35 36 //Method 的使用37 Method method1= clazz.getDeclaredMethod("getName");38 System.out.println(method1.getModifiers());註解(Annotation)
註解的定義形式:
1 @interface 註解名{2 <成員類型> <成員名稱>() default "<預設值>";3 ……4 }
下面這個例子定義了一個註解,註解名為MyAnotation,註解包含一個成員是int型的value()。@Retention表示註解的應用範圍,註解的應用範圍通常用RetentionPolicy中的枚舉常量表示,這個例子中用的RetentionPolicy中的RUNTIME常量,表示在運行時載入註解到虛擬機器,有效範圍最大。@Target表示次註解適用於變數類型,並且可為int類型的變數賦值。
此圖片摘自部落格園的……@黑色幽默Lion
1 @Retention(RetentionPolicy.RUNTIME)2 @Target(ElementType.FIELD)3 public @interface MyAnotation {4 int value();5 }
1 import java.lang.reflect.Field; 2 3 public class Student { 4 private String name; 5 @MyAnotation(value =18) 6 private int age; 7 Student(){ 8 Class clazz = Student.class; 9 try {10 //擷取age屬性對象11 Field field = clazz.getDeclaredField("age");12 //建立註解對象,擷取age屬性的註解 13 MyAnotation ma = field.getAnnotation(MyAnotation.class);14 //判斷獲得的註解對象是否為空白15 if(ma!=null){16 //為age賦值17 int i = ma.value();18 field.setAccessible(true);19 field.set(this, i);20 field.setAccessible(false); 21 } 22 } catch (NoSuchFieldException | SecurityException e) {23 // TODO Auto-generated catch block24 e.printStackTrace();25 } catch (IllegalArgumentException e) {26 // TODO Auto-generated catch block27 e.printStackTrace();28 } catch (IllegalAccessException e) {29 // TODO Auto-generated catch block30 e.printStackTrace();31 } 32 } 33 public String getName() {34 return name;35 }36 public void setName(String name) {37 this.name = name;38 }39 public int getAge() {40 return age;41 }42 public void setAge(int age) {43 this.age = age;44 } 45 }
Java中的反射和註解