Java中反射代碼執行個體

來源:互聯網
上載者:User

標籤:ble   pack   star   函數   try   建立   代碼   main   package   

我們建立一個Person類,對此進行反射操作。

package myReflection;public class Person {    private String name;    private String id;    Person(){}    /**     * @param name     * @param id     */    public Person(String name, String id) {        super();        this.name = name;        this.id = id;    }    /**     * @return the name     */    public String getName() {        return name;    }    /**     * @param name the name to set     */    public void setName(String name) {        this.name = name;    }    /**     * @return the id     */    public String getId() {        return id;    }    /**     * @param id the id to set     */    public void setId(String id) {        this.id = id;    }    }

分別對constructor‘、屬性和方法反射

test1====無參數建構函式

test2 ====有參數建構函式

test3=====屬性

test4=====方法

package myReflection;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;import org.junit.Test;public class useReflection {    public static void main(String[] args) throws ClassNotFoundException {        // TODO Auto-generated method stub        Class c1=Person.class;        Class c2=new Person().getClass();        Class c3=Class.forName("myReflection.Person");            }    @Test    public void test4() throws Exception{        Class c=Class.forName("myReflection.Person");        Person p=(Person) c.newInstance();        Method m=c.getDeclaredMethod("setName", String.class);                m.invoke(p, "Lannister");        System.out.println(p.getName());    }    @Test    public void test3() throws Exception{        try {            Class c=Class.forName("myReflection.Person");            Person p=(Person) c.newInstance();            Field f1=c.getDeclaredField("name");            f1.setAccessible(true);            f1.set(p, "Stark");            System.out.println(f1.get(p));                    } catch (Exception e) {            // TODO: handle exception        }    }    @Test    public void test2() throws Exception    {        Class c=Class.forName("myReflection.Person");        Constructor cs=c.getConstructor(String.class,String.class);        Person p1=(Person)cs.newInstance("Jon","Snow");        System.out.println(p1.getId()+" "+p1.getName());    }    @Test    public void test1() throws Exception{        Class cl3=Class.forName("myReflection.Person");        Person p =(Person)cl3.newInstance();        p.setName("Tully");        System.out.println(p.getName());    }}

 

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.