Java 反射詳解

來源:互聯網
上載者:User

標籤:end   int   except   span   const   color   str   imp   java反射   

Java反射機制可以讓我們在編譯期(Compile Time)之外的運行期(Runtime)獲得任何一個類的位元組碼。包括介面、變數、方法等資訊。還可以讓我們在運行期執行個體化對象,通過調用get/set方法擷取變數的值。

一下我們通過幾個小demo來瞭解java反射的相關知識

首先,我們先定義一個類,以便我們後續能通過反射擷取到這個類基本資料

 1 class Person { 2     private int age; 3     private String name; 4  5     public Person() { 6  7     } 8  9     public Person(int age, String name) {10         this.age = age;11         this.name = name;12     }13 14     public int getAge() {15         return age;16     }17 18     public void setAge(int age) {19         this.age = age;20     }21 22     public String getName() {23         return name;24     }25 26     public void setName(String name) {27         this.name = name;28     }29 30     @Override31     public String toString() {32         return "Person{" +33                 "age=" + age +34                 ", name=‘" + name + ‘\‘‘ +35                 ‘}‘;36     }37 }38 39 class SuperMan extends Person implements ActionInterface {40     private boolean BlueBriefs;41 42     public void fly() {43         System.out.println("超人會飛耶~~");44     }45 46     public boolean isBlueBriefs() {47         return BlueBriefs;48     }49 50     public void setBlueBriefs(boolean blueBriefs) {51         BlueBriefs = blueBriefs;52     }53 54     public void wc(){55         System.out.println("超人會噓噓");56     }57 58 59     @Override60     public void walk(int m) {61         // TODO Auto-generated method stub62         System.out.println("超人會走耶~~走了" + m + "米就走不動了!");63     }64 }65     interface ActionInterface{66         public void walk(int m);67     }

一個Person類,子類是superman類,子類還實現了ActionInterface 介面

 

 

1.擷取類

//可能拋出 ClassNotFoundExceptionClass<?> reflet = Class.forName("com.example.thread.demo.ReflectDemo");        System.out.println("包名"+reflet.getPackage().getName()+"類名:"+reflet.getName());
包名com.example.thread.demo類名:com.example.thread.demo.ReflectDemo

2.執行個體化對象

        ReflectDemo reflectDemo = (ReflectDemo) reflet.newInstance();

3.得到Person的建構函式

1 Constructor<?>[] constructors = reflect.getConstructors();2         Person person = (Person) constructors[0].newInstance();3         person.setAge(12);4         person.setName("zs");5         System.out.println(person.toString());

Person類有兩個構造器,第二行表示的是取第一個構造器也就是無參構造器並執行個體化對象

4.擷取類中的方法

 1 Class<?> bClass = Class.forName("com.example.thread.demo.SuperMan"); 2  3         //包括公用、保護、預設(包)訪問和私人方法,但不包括繼承的方法。當然也包括它所實現介面的方法。 4         Method[] declaredMethods = bClass.getDeclaredMethods(); 5  6         //所有公用(public)方法包括其繼承類的公用方法,當然也包括它所實現介面的方法。 7         Method[] methods = bClass.getMethods(); 8  9         for(Method method:declaredMethods){10             System.out.println("Demo6,取得SuperMan類的方法:");11             System.out.println("函數名:" + method.getName());12             System.out.println("函數傳回型別:" + method.getReturnType());13             System.out.println("函數存取修飾詞:" + Modifier.toString(method.getModifiers()));14             System.out.println("函數代碼寫法: " + method);15         }

5.擷取變數

Field[] declaredFields = aClass.getDeclaredFields();

6.擷取變數上的註解

1 Annotation[] annotations = field.getAnnotations();2             for(Annotation annotation:annotations){3                 Class<? extends Annotation> aClass2 = annotation.annotationType();4                 String name1 = aClass2.getName();5                 System.out.println(name1);6             }

 

 

 

好了,其實反射的api就那麼幾個,重點還是在於反射的思想,反射在java的應用中更多的是在架構層面和註解的使用上,這方面我們後續在做進一步的瞭解

 

Java 反射詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.