java反射常用類

來源:互聯網
上載者:User

標籤:構造方法   ati   inf   object   item   const   屬性   throws   tco   

  1. 測試實體類
public class TestClass {    public String classInfo;    public String getClassInfo() {        return classInfo;    }    public TestClass(String classInfo) {        this.classInfo = classInfo;        System.out.println("invoke this param classInfo constructor.");    }    public TestClass()    {        System.out.println("invoke this default constructor.");    }    public void setClassInfo(String classInfo) {        this.classInfo = classInfo;    }    static {        System.out.println("init first class.");    }    public void showinfo()    {        System.out.println("this is default show info method.");    }    public void showinfo(String info)    {        System.out.println("this is have param method.");        System.out.println("param context:"+info);    }}
  1.   測試實作類別
import org.junit.Test;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;public class getClassInfoTest {    @Test    public void testDemo() throws Exception    {        /**         * 1.實現反射的第一步:擷取對象的類型資訊,java中指存放類型資訊的.class檔案         * 2.在這裡是擷取對象的所有構造方法.         * 3.找到需要的構造方法.         * 4.調用指定構造方法,建立對象.         */        Class clazz=Class.forName("TestClass");        /**         * 擷取構造方法         * 調用newInstance(...) 相當於 new TestClass(...)         */        Constructor[] constructors=clazz.getConstructors();        System.out.println("constructor item:"+‘\n‘);        arrayToStringItem(constructors);        Object test=constructors[0].newInstance();        Object test2=constructors[1].newInstance("hello");        /**         * 擷取屬性         */        Field[] fields=clazz.getFields();        System.out.println("field item:"+‘\n‘);        arrayToStringItem(fields);        Field field=fields[0];        System.out.println("field name:"+field.getName());        System.out.println("get dafault constructor field value:"+field.get(test));        System.out.println("get have param constructor field value:"+field.get(test2));        /**         * 擷取方法         */        Method[] methods=clazz.getMethods();        System.out.println("method item:"+‘\n‘);        arrayToStringItem(methods);        System.out.println("invoke "+methods[0].getName()+" get return value:"+methods[0].invoke(test2));    }    public void arrayToStringItem(Object[] args)    {        for (Object c:args)        {            System.out.println(c);        }    }}

  

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.