Java反射基礎

來源:互聯網
上載者:User

標籤:oid   str   for   getname   私人   載器   system   iat   http   

1.反射的描述java的反射機制是在運行狀態中,對於任意類,都能知道這個類的所有方法和屬性,對於任意對象的都能調用他們的方法和屬性。這種動態調用的機制被稱為反射。 要想解析一個類,必須得到這個類的位元組碼檔案對象,而要解析的就是Class類。所有得擷取每個類的位元組碼的Class類型的對象。 java對象載入的過程。new對象的時候,jvm從位元組碼檔案中找到這個對象的class檔案載入到記憶體中,形成對象空間和建立Class對象,再new相同的對象,不能再載入,java反射的本質就是擷取Class對象反向擷取對象的所有資訊。 解釋class對象的載入過程。  Class 類的執行個體表示正在啟動並執行 Java 應用程式中的類和介面。也就是jvm中有N多的執行個體每個類都有該Class對象。(包括基礎資料型別 (Elementary Data Type))Class 沒有公用構造方法。Class 對象是在載入類時由 JAVA 虛擬機器以及通過調用類載入器中的defineClass方法自動構造的。也就是這不需要我們自己去處理建立,JVM已經幫我們建立好了。 知道我們主要通過擷取Class類對象來反向擷取類對象的所有資訊。  2.擷取Class的3種方式:new Person().getClass();Person.class;Class.forName("com.Person");  3.對象的描述類的構成:構造方法,屬性,方法。 Class對象描述這三個用Constructor Field,Method 類描述。 Constructor查看下面的例子:package com.JavaTest3;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException; public class Demo {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {Class<Student> studentClass = Student.class;Student student = new Student();Class<? extends Student> aClass = student.getClass();Class<?> aClass1 = Class.forName("com.JavaTest3.Student"); System.out.println(studentClass == aClass1); //trueSystem.out.println(studentClass == aClass); // trueSystem.out.println(aClass1 == aClass); // trueConstructor<?>[] constructors = aClass1.getConstructors(); // 擷取所以的public方法Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();// 擷取所以的方法,私人的,受保護的for (Constructor c : constructors) {System.out.println(c);}for(Constructor c1 : declaredConstructors) {System.out.println(c1);}// 擷取類的無參構造方法執行。Constructor<?> constructor = aClass1.getConstructor(null);Object o = constructor.newInstance(); // 擷取類的有參方法執行Constructor<?> constructor1 = aClass1.getConstructor(char.class);constructor.setAccessible(true);Object o1 = constructor1.newInstance(‘男‘);}} Field類查看下面的例子: package com.JavaTest3;import java.lang.reflect.Field;public class Demo1 {public static void main(String[] args) {try {Class<?> aClass = Class.forName("com.JavaTest3.Student1");Field[] fields = aClass.getFields();for (Field f : fields){System.out.println(f);}for(Field f:aClass.getDeclaredFields()){System.out.println(f);}Field f = aClass.getDeclaredField("name");Object o = aClass.getDeclaredConstructor(null).newInstance();f.setAccessible(true); // 暴力訪問f.set(o,"周星馳");Student1 student = (Student1)o;System.out.println(student.getName());}catch (Exception ex){ex.printStackTrace();}}}Method類查看下面的例子:package com.JavaTest3; import java.lang.reflect.Method; public class Demo2 {public static void main(String[] args) {try {Class<?> aClass = Class.forName("com.JavaTest3.Student2");for(Method m:aClass.getMethods()){System.out.println(m);}for(Method m:aClass.getDeclaredMethods()) {System.out.println(m);}Object o = aClass.getConstructor().newInstance();Method show1 = aClass.getDeclaredMethod("show1", String.class);show1.invoke(o, "周小青");Method show2 = aClass.getDeclaredMethod("show2", null);show2.invoke(o);Method show3 = aClass.getDeclaredMethod("show3", null);show3.invoke(o);Method show4 = aClass.getDeclaredMethod("show4", int.class); // 私人的show4.setAccessible(true);show4.invoke(o,123);}catch (Exception ex) {ex.printStackTrace();}}} 總結: 構造方法擷取一般使用getDeclaredConstructors,屬性一般使用getDeclaredFields 方法使用aClass.getDeclaredMethods 訪問私人方法設定Accessible為true. 

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.