JAVA的反射機制學習筆記(二),java學習筆記

來源:互聯網
上載者:User

JAVA的反射機制學習筆記(二),java學習筆記

             上次寫JAVA的反射機制學習筆記(一)的時候,還是7月22號,這些天就瞎忙活了,自己的步伐完全被打亂了~不能繼續被動下去,得重新找到自己的節奏。

4、擷取類的Constructor


通過反射機製得到某個類的構造器,然後調用該構造器建立該類的一個執行個體 

Class<T>類提供了幾個方法擷取類的構造器。

public Constructor<T> getConstructor(Class<?>... parameterTypes)

返回一個 Constructor 對象,它反映此 Class 對象所表示的類的指定公用構造方法

public Constructor<?>[] getConstructors()

返回一個包含某些 Constructor 對象的數組,這些對象反映此 Class 對象所表示的類的所有公用構造方法

public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)

返回一個 Constructor 對象,該對象反映此 Class 對象所表示的類或介面的指定構造方法

public Constructor<?>[] getDeclaredConstructors()

返回 Constructor 對象的一個數組,這些對象反映此 Class 對象表示的類聲明的所有構造方法。它們是公用、保護、預設(包)訪問和私人構造方法

5、建立類的執行個體

通過反射機制建立新類的執行個體,有幾種方法可以建立

調用無自變數ctor

1、調用類的Class對象的newInstance方法,該方法會調用對象的預設構造器,如果沒有預設構造器,會調用失敗.

Class<?> classType = ExtendType.class;

Object inst = classType.newInstance();

System.out.println(inst);

輸出:

Type:Default Constructor

ExtendType:Default Constructor

com.quincy.ExtendType@d80be3

 

2、調用預設Constructor對象的newInstance方法

Class<?> classType = ExtendType.class;

Constructor<?> constructor1 = classType.getConstructor();

Object inst = constructor1.newInstance();

System.out.println(inst);

輸出:

Type:Default Constructor

ExtendType:Default Constructor

com.quincy.ExtendType@1006d75

調用帶參數ctor

3、調用帶參數Constructor對象的newInstance方法

Constructor<?> constructor2 =

classType.getDeclaredConstructor(int.class, String.class);

Object inst = constructor2.newInstance(1, "123");

System.out.println(inst);

輸出:

Type:Default Constructor

ExtendType:Constructor with parameters

com.quincy.ExtendType@15e83f9

class Dog {    public String name;    public int age;    public int weigh;    public void bark(String name){        System.out.println(name);};    public void run(){};    Dog(){}    Dog(String name) {        this.name = name;    }    Dog(String name,int age) {        this.name = name;        this.age = age;    }}

        // 取得位元組碼,這種取法 更安全、更高效。        Class clazzDog = Dog.class;        System.out.println("-----------------Constructor-------------------");        // 使用ggetDeclaredConstructors擷取構造器        Constructor<?>[] constructors2 = clazzDog.getDeclaredConstructors();        for (Constructor<?> m : constructors2)        {            System.out.println(m);            Class<?>[] types = m.getParameterTypes();            for (Class<?> type : types) {                System.out.println(type);            }        }        System.out.println("-----------------new instance-------------------");        Constructor constructor = clazzDog.getDeclaredConstructor(String.class,int.class);        Dog dog = (Dog)constructor.newInstance("換換",2);        System.out.println(dog.name);

-----------------Constructor-------------------com.reflection.Dog()com.reflection.Dog(java.lang.String)class java.lang.Stringcom.reflection.Dog(java.lang.String,int)class java.lang.Stringint-----------------new instance-------------------換換

6、調用類的函數

通過反射擷取類Method對象,調用Field的Invoke方法調用函數。

        // 取得位元組碼,這種取法 更安全、更高效。        Class clazzDog = Dog.class;        Object object = clazzDog.newInstance();        Method method = clazzDog.getDeclaredMethod("bark",String.class);        method.invoke(object, "狗叫");
狗叫

7、設定/擷取類的屬性值

通過反射擷取類的Field對象,調用Field方法設定或擷取值

        // 取得位元組碼,這種取法 更安全、更高效。        Class clazzDog = Dog.class;        Object object = clazzDog.newInstance();        Field field =  clazzDog.getDeclaredField("name");        field.set(object, "歡歡");        System.out.println(((Dog)object).name);
歡歡





java反射機制詳解

在Java運行時刻,能否知道一個類的屬性方法並調用改動之?對於任意一個對象,能否知道他的所屬類,並調用他的方法?答案是肯定的。這種動態擷取資訊及動態調用方法的機制在Java中稱為“反射”(reflection)。
Java反射機制主要提供以下功能:
在運行時判斷任意一個對象所屬的類;
在運行時構造任意一個類的對象;
在運行時判斷任意一個類所具有的成員變數和方法;
在運行時調用任意一個對象的方法。
Reflection 是Java被視為動態(或准動態)語言的一個關鍵性質。這個機制允許程式在運行時透過Reflection APIs取得任何一個已知名稱的class的內部資訊,包括其modifiers(諸如public, static 等等)、superclass(例如Object)、實現之interfaces(例如Serializable),也包括fields和methods 的所有資訊,並可於運行時改變fields內容或調用methods。
一般而言,開發人員社群說到動態語言,大致認同的一個定義是:“程式運行時,允許改變程式結構或變數類型,這種語言稱為動態語言”。
在JDK中,主要由以下類來實現Java反射機制,這些類都位於java.lang.reflect包中:
Class類:代表一個類;
Field 類:代表類的成員變數(成員變數也稱為類的屬性);
Method類:代表類的方法;
Constructor 類:代表類的構造方法;
Array類:提供了動態建立數組,以及訪問數組的元素的靜態方法;

至於全部的你可以看看參考資料。我看這個資料不錯
參考資料:apps.hi.baidu.com/share/detail/24992875
 
Java Reflection (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.