Java記錄 -86- Reflection API的使用樣本進階2

來源:互聯網
上載者:User

Java記錄 -86- Reflection API的使用樣本進階2
擷取指定類下的資訊:所有方法和屬性 

public class DumpClassInfo {    public static void main(String[] args) throws Exception{        //Reflection API的基本作用        Class<?> classtype = Class.forName("my.reflect.Customer");        Method[] methods = classtype.getDeclaredMethods();        //擷取指定類下的所有方法,包含私人方法。(運行時所有方法)        for(Method method : methods){            System.out.println(method);        }        Field[] fields = classtype.getDeclaredFields();        //擷取指定類下的所有屬性,包含私人屬性。(運行時所有屬性)        for(Field field : fields){            System.out.println(field);        }    }}class Customer{    private long id;    private String name;    private int age;         public Customer(){}         public Customer(String name, int age){        this.name = name;        this.age = age;    }    public long getId() {        return id;    }    public void setId(long id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

 

 以上程式輸出結果: 
public void my.reflect.Customer.setId(long)public int my.reflect.Customer.getAge()public void my.reflect.Customer.setAge(int)public java.lang.String my.reflect.Customer.getName()public long my.reflect.Customer.getId()public void my.reflect.Customer.setName(java.lang.String)private long my.reflect.Customer.idprivate java.lang.String my.reflect.Customer.nameprivate int my.reflect.Customer.age

 

 

聯繫我們

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