黑馬程式員——反射機制,黑馬程式員反射

來源:互聯網
上載者:User

黑馬程式員——反射機制,黑馬程式員反射

------<a href="http://www.itheima.com" target="blank">Java培訓、Android培訓、iOS培訓、.Net培訓</a>、期待與您交流! -------

反射機制

下面我們一起來探討一下java的反射機制吧!功能太強大了,哈哈!

 (一).  定義

  Reflection(反射)是Java被視為動態語言的關鍵,反射機制允許程式在執行期藉助於Reflection API取得任何類的內部資訊,並能直接操作任意對象的內部屬性及方法。

1.1 主要功能

  • 在運行時判斷任意一個對象所屬的類
  • 在運行時構造任意一個類的對象
  • 在運行時判斷任意一個類所具有的成員變數和方法
  • 在運行時調用任意一個對象的方法
  • 產生動態代理

1.2 應用範圍

  在web應用中不是很多,但反射機制是如今很多流行架構的實現基礎,包括Spring、Hibernate等。

1.3 注意

  Java通過Reflection API來完成反射機制,在java.lang.reflect包中有Field、Method、Constructor三個類分別用於描述類的屬性、方法、構造方法。

1.4 Constructor類 

  Constructor類用於表示類的構造方法,通過調用Class對象的getConstructor()方法就能擷取當前類的構造方法的集合。

1.4.1 Constructor類的方法

  String getName()————返回構造方法的名稱
  Class[] getParameterTypes()————返回當前構造方法的參數類型
  int getModifiers()————返回修飾符的整型標識

1.4.2 實踐

上代碼示範getConstructor()方法擷取指定類的構造方法

 1 package com.itsuper.p2.reflection; 2  3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.Modifier; 5  6 public class ConstructorReflectionDemo { 7  8     public static void main(String[] args) { 9         String name = "java.util.Date";10         try {11             Class c1 = Class.forName(name);12             System.out.println("class " + name + "{");13             getConstructors(c1);14             System.out.println("}");15         } catch (ClassNotFoundException e) {16             System.out.println("Check name of the class!");17         }18     }19 20     public static void getConstructors(Class c1) {21         //返回聲明的所有構造方法包括私人的和受保護的,但不包括超類構造方法22         Constructor[] cons = c1.getDeclaredConstructors();23         for(int i=0;i<cons.length;i++){24             Constructor c = cons[i];25             //返回構造方法的名稱26             String name = c.getName();27             //通過Modifier類擷取修飾符28             System.out.print("   " + Modifier.toString(c.getModifiers()));29             System.out.print(" " + name + "(");30             //擷取構造方法的參數31             Class[] paramTypes = c.getParameterTypes();32             //列印構造方法的參數33             for(int j=0;j<paramTypes.length;j++){34                 if(j>0){35                     System.out.print(",");36                 }37                 System.out.print(paramTypes[j].getName());38             }39             System.out.println(")");40         }41     }42 }

1.5 Method類

  Method類提供關於類或介面上某個方法的資訊,它是用來封裝反射類方法的一個類。

1.5.1 Method類方法

  String getName()————返回方法的名稱
  Class[] getParameterTypes()————返回當前方法的參數類型
  int getModifiers()————返回修飾符的整型標識
  Class getReturnType()————返回當前方法的傳回型別

1.5.2 實踐

代碼示範getMethod()方法擷取指定類的所有的方法資訊。

 1 package com.itsuper.p2.reflection; 2  3 import java.lang.reflect.Method; 4 import java.lang.reflect.Modifier; 5  6 public class MethodReflectionDemo { 7     public static void main(String[] args) { 8         String  name = "java.util.Date"; 9         try { 10             //返回指定字串所代表的類的Class對象11             Class c1 = Class.forName(name);12             System.out.println("class " + name + "\n{");13             getMethods(c1);14             System.out.println("}");15         } catch (ClassNotFoundException e) {16             System.out.println("Check name of the Class");17         }18         System.exit(0);19     }20 21     public static void getMethods(Class c1) {22         //返回聲明的所有的方法包括私人的和受保護的,但不包括超類方法23         Method[] methods = c1.getDeclaredMethods();24         //返回公用方法,包括從父類繼承的公用方法25 //        Method[] methods1 = c1.getMethods();26         for(int i=0;i<methods.length;i++){27             //擷取單個method方法28             Method m = methods[i];29             //擷取當前方法的傳回型別30             Class retype = m.getReturnType();31             //擷取方法名32             String name = m.getName();33             System.out.print("   " + Modifier.toString(m.getModifiers()));//修飾符34             System.out.print(" " + retype.getName() + " " + name + "(");//傳回型別、方法名35             //列印參數資訊36             Class[] paramtypes = m.getParameterTypes();37             for(int j=0;j<paramtypes.length;j++){38                 if(j>0){39                     System.out.print(",");40                 }41                 //列印參數名42                 System.out.print(paramtypes[j].getName());43             }44             System.out.println(");");45         }46     }47 }

1.6 Field類

  Field類提供有關類或介面的屬性資訊。

1.6.1 Field類的方法

  String getName()————返回方法的名稱
  Class[] getType()————返回當前屬性的參數列表

1.6.3 實踐

直接上代碼,通過代碼理解getField()方法用於擷取指定類或介面定義的屬性資訊。

 1 package com.itsuper.p2.reflection; 2  3 import java.lang.reflect.Field; 4 import java.lang.reflect.Modifier; 5  6 public class FieldReflectionDemo { 7  8     public static void main(String[] args) { 9         String name = "java.util.Date";10         try {11             Class c1 = Class.forName(name);12             System.out.println("class " + name + "\n{");13             getFields(c1);14             System.out.println("}");15         } catch (ClassNotFoundException e) {16             System.out.println("Check name of the class !");17         }18     }19 20     public static void getFields(Class c1) {21         //返回聲明的所有的屬性包括私人的和受保護的,但不包括超類屬性22         Field[] fields= c1.getDeclaredFields();23         for(int i=0;i<fields.length;i++){24             Field field = fields[i];25             Class type = field.getType();26             String name = field.getName();27             System.out.print(" "+ Modifier.toString(field.getModifiers()));28             System.out.println(" " + type.getName() + " " + name + ";");29             30         }    31     }32 }

要注意:Field類的getDeclaredFields()方法將返回包括私人的和受保護的所有屬性定義,但不包括父類的屬性;getFields()方法返回的屬性列表將包括從父類繼承的公用屬性。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.