java反射——構造方法

來源:互聯網
上載者:User

標籤:位元組碼   ring   語言   重要   技術   簡單的   dex   not   nis   

大家都知道反射技術在Java裡面時非常重要的一個技術點,因為Java好多架構的編寫都是基於反射的,別的不多說,spring架構裡面的IOC就是基於反射實現。那麼什麼是反射呢?JAVA反射機制是在運行狀態中,對於任意一個類,都能夠知道這個類的所有屬性和方法;對於任意一個對象,都能夠調用它的任意方法和屬性;這種動態擷取資訊以及動態調用對象方法的功能稱為java語言的反射機制。說白了,反射就是載入類,並且反射出類的各個組成部分的一種實現方式。

 那麼我們如何去載入一個類呢?可以通過以下幾種方式實現。

public void testLoadClass() throws ClassNotFoundException {        // 擷取位元組碼類,可以採用一下三種方法        // 相當於載入Person.class檔案加入到記憶體中        Class clazz = Class.forName("com.day09.Person");        Class clazz1 = new Person().getClass();        Class clazz2 = Person.class;    }

Java是一種物件導向的語言,而Java建立對象的一般都是通過new+構造方法來建立對象的,所以我們先來看看如何反射類的構造方法。同時,因為Java的方法重載機制,所以構造方法也存在多種性質,下面我們的例子將示範幾種最常見的構造方法,並利用反射來用他們來建立對象。

1.定義一個簡單的Person類

package com.day09;import java.util.List;public class Person {    public Person() {        System.out.println("Person");    }    public Person(String name) {        System.out.println("person name");    }    public Person(String name, int password) {        System.out.println("Person name password");    }    private Person(List list) {        System.out.println("Person list");    }}

2.利用Junit來示範進行示範測試

package com.day09;import java.util.List;import java.lang.reflect.Constructor;import java.util.ArrayList;import org.junit.Test;/** * 放射就是載入類,並解剖出類的各個組成部分 類的組成部分: 1.構造方法:建立對象 2.屬性欄位:封裝資料 3.屬性方法:封裝功能 *  * @author Administrator * */public class ReflectDemo {    @Test    public void testLoadClass() throws ClassNotFoundException {        // 擷取位元組碼類,可以採用一下三種方法        // 相當於載入Person.class檔案加入到記憶體中        Class clazz = Class.forName("com.day09.Person");        Class clazz1 = new Person().getClass();        Class clazz2 = Person.class;    }    /**     * public Person() 反射無參構造方法     *      * @throws Exception     */    @Test    public void reflectConstructNoParameter() throws Exception {        // 將person類載入到記憶體中        Class<?> clazz = Class.forName("com.day09.Person");        // 傳入參數,擷取對應的建構函式        Constructor<?> personCon = clazz.getConstructor(null);        // 傳入參數開始建立對象        Person p = (Person) personCon.newInstance(null);    }    /**     * 反射帶有一個參數構造方法構造方法 public Person(String name)     *      * @throws Exception     */    @Test    public void reflectConstructOneParameter() throws Exception {        // 將person類載入到記憶體中        Class<?> clazz = Class.forName("com.day09.Person");        // 傳入參數,擷取對應的建構函式,這裡面要傳入一個參數類型,來擷取對應參數的構造方法        Constructor<?> personCon = clazz.getConstructor(String.class);        // 傳入參數開始建立對象        Person p = (Person) personCon.newInstance("zhangsan");    }    /**     * 反射帶有多個參數構造方法構造方法 Person(String name, int password)     *      * @throws Exception     */    @Test    public void reflectConstructManyParameter() throws Exception {        // 將person類載入到記憶體中        Class<?> clazz = Class.forName("com.day09.Person");        // 傳入參數,擷取對應的建構函式,這裡面要傳入一個參數類型,來擷取對應參數的構造方法        Constructor<?> personCon = clazz.getConstructor(String.class, int.class);        // 傳入參數開始建立對象        Person p = (Person) personCon.newInstance("zhangsan", 23);    }    /**     * 反射私人的構造方法構造方法 private Person(List list)     *      * @throws Exception     */    @Test    public void reflectPrivateConstruct() throws Exception {        // 將person類載入到記憶體中        Class<?> clazz = Class.forName("com.day09.Person");        // 傳入參數,擷取對應的建構函式,這裡面要傳入一個參數類型,來擷取對應參數的構造方法        // 這裡因為該構造方法是私人的,所以用用getDeclaredConstructor才能獲得構造方法對象        Constructor<?> personCon = clazz.getDeclaredConstructor(List.class);        // 要使用私人的屬性成員,還需要放開許可權,設為可見        personCon.setAccessible(true);        // 傳入參數開始建立對象        Person p = (Person) personCon.newInstance(new ArrayList());    }    /**     * 通過class類,可以直接通過無參的構造方法建立對象     *      * @throws Exception     */    @Test    public void testNewObj() throws Exception {        // 將person類載入到記憶體中        Class<?> clazz = Class.forName("com.day09.Person");        Person p = (Person) clazz.newInstance();    }}

至此,我們已將常見的幾種通過反射獲得構造方法,並建立對象示範完畢,有不足的地方,希望大家多多提意見!

 

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.