淺談Java反射機制 之 獲取類的 方法 和 屬性(包括構造函數)


上一篇 獲取 的字節碼文件  我們講到了獲取類的字節碼文件的三種方法

第三種方法通過getClass("全路徑名")獲取字節碼文件最符合要求

1、獲取構造方法

先貼上我們要獲取的類結構

import java.util.Date;

public class Student {

    private String name;

    private Integer age;

    private Date Birthday;

    public Student(){

    }

    private Student(String name){
        this.name=name;
    }

    private Student(Integer age){
        this.age=age;
    }

    private Student(Date Birthday){
        this.Birthday=Birthday;
    }

    public Student(String name,Integer age){
        this.name=name;
        this.age=age;
    }

    public Student(Integer age,String name){
        this.name=name;
        this.age=age;
    }

    public Student(String name,Date Birthday){
        this.name=name;
        this.Birthday=Birthday;
    }

    public Student(Date Birthday,String name){
        this.name=name;
        this.Birthday=Birthday;
    }

    public Student(Integer age,Date Birthday){
        this.age=age;
        this.Birthday=Birthday;
    }

    public Student(Date Birthday,Integer age){
        this.age=age;
        this.Birthday=Birthday;
    }

    public Student(String name,Integer age,Date Birthday){
        this.age=age;
        this.name=name;
        this.Birthday=Birthday;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return Birthday;
    }

    public void setSex(Date sex) {
        this.Birthday = Birthday;
    }
}

通過 getConstructors()   getDeclaredConstructors()  getConstructor()  getDeclaredConstructor()這四個方法獲取各種構造方法

import java.lang.reflect.Constructor;
import java.util.Date;

public class Test04 {
    public static void main(String[] args) throws ClassNotFoundException,NoSuchMethodException{

        //加載Class對象
        //會報出不存在該類的異常
        Class c=Class.forName("com.reflection.model.Student");

        //獲取所有公用構造方法
        System.out.println("================獲取所有公共的構造方法=================");
        Constructor[] constructors=c.getConstructors();
        for (Constructor constructor:constructors) {
            System.out.println("公共的構造方法:"+constructor);
        }
        //獲取所有構造方法
        System.out.println("================獲取所有的構造方法=================");
        Constructor[] declaredconstructors=c.getDeclaredConstructors();
        for (Constructor constructor:declaredconstructors) {
            System.out.println("所有構造方法:"+constructor);
        }

        //獲取公有&無參構造方法
        System.out.println("================獲取公有&無參構造方法=================");

        //會報出沒有該方法的異常
        Constructor constructor1=c.getConstructor(null);
        System.out.println("公有&無參構造方法:"+constructor1);

        //獲取公有&有參構造方法
        System.out.println("================獲取公有&有參構造方法=================");

        //會報出沒有該方法的異常
        Constructor constructor2=c.getConstructor(new Class[]{String.class,Integer.class, Date.class});
        System.out.println("公有&有參構造方法:"+constructor2);
        Constructor constructor3=c.getConstructor(new Class[]{String.class,Integer.class});
        System.out.println("公有&有參構造方法:"+constructor3);

        //獲取私有&有參構造方法
        System.out.println("================獲取私有&有參構造方法=================");

        //會報出沒有該方法的異常
        Constructor declaredconstructor1=c.getDeclaredConstructor(new Class[]{String.class});
        System.out.println("私有&有參構造方法:"+declaredconstructor1);
    }
}

結果:

================獲取所有公共的構造方法=================
公共的構造方法:public com.reflection.model.Student(java.lang.Integer,java.lang.String)
公共的構造方法:public com.reflection.model.Student(java.lang.String,java.util.Date)
公共的構造方法:public com.reflection.model.Student(java.util.Date,java.lang.String)
公共的構造方法:public com.reflection.model.Student(java.lang.Integer,java.util.Date)
公共的構造方法:public com.reflection.model.Student(java.util.Date,java.lang.Integer)
公共的構造方法:public com.reflection.model.Student(java.lang.String,java.lang.Integer,java.util.Date)
公共的構造方法:public com.reflection.model.Student()
公共的構造方法:public com.reflection.model.Student(java.lang.String,java.lang.Integer)
================獲取所有的構造方法=================
所有構造方法:public com.reflection.model.Student(java.lang.Integer,java.lang.String)
所有構造方法:public com.reflection.model.Student(java.lang.String,java.util.Date)
所有構造方法:public com.reflection.model.Student(java.util.Date,java.lang.String)
所有構造方法:public com.reflection.model.Student(java.lang.Integer,java.util.Date)
所有構造方法:public com.reflection.model.Student(java.util.Date,java.lang.Integer)
所有構造方法:public com.reflection.model.Student(java.lang.String,java.lang.Integer,java.util.Date)
所有構造方法:public com.reflection.model.Student()
所有構造方法:private com.reflection.model.Student(java.lang.String)
所有構造方法:private com.reflection.model.Student(java.lang.Integer)
所有構造方法:private com.reflection.model.Student(java.util.Date)
所有構造方法:public com.reflection.model.Student(java.lang.String,java.lang.Integer)
================獲取公有&無參構造方法=================
公有&無參構造方法:public com.reflection.model.Student()
================獲取公有&有參構造方法=================
公有&有參構造方法:public com.reflection.model.Student(java.lang.String,java.lang.Integer,java.util.Date)
公有&有參構造方法:public com.reflection.model.Student(java.lang.String,java.lang.Integer)
================獲取私有&有參構造方法=================
私有&有參構造方法:private com.reflection.model.Student(java.lang.String)

結論:

getConstructors()返回所有public的構造器。

getDeclaredConstructors()返回所有privatepublic構造器。

getConstructor()返回指定參數類型public的構造器。 

getDeclaredConstructor()返回指定參數類型的privatepublic構造器。

 2、獲取類屬性

將 1 中的 Student 類中的  name 字段設為 public

public String name;

通過 getFields()   getDeclaredFields()  getField()  getDeclaredField()這四個方法獲取各種字段

import java.lang.reflect.Field;
public class Test05 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {

        //加載Class對象
        //會報出不存在該類的異常
        Class c=Class.forName("com.reflection.model.Student");

        //獲取所有公用公共字段
        System.out.println("================獲取所有公共字段=================");
        Field[] fields=c.getFields();
        for (Field field:fields) {
            System.out.println("公共字段:"+field);
        }
        //獲取所有字段
        System.out.println("================獲取所有的字段(公共的、私有的)=================");
        Field[] declaredFields=c.getDeclaredFields();
        for (Field declaredfield:declaredFields) {
            System.out.println("所有字段:"+declaredfield);
        }

        System.out.println("================根據字段名獲取公共字段=================");
        //根據字段名獲取公共字段
        Field field1=c.getField("name");
        System.out.println("根據字段名獲取公共字段:"+field1);

        System.out.println("================根據字段名私有字段=================");
        //根據字段名獲取公共字段
        Field field2=c.getDeclaredField("age");
        System.out.println("根據字段名獲取公共字段:"+field2);

    }
}

結果:

================獲取所有公共字段=================
公共字段:public java.lang.String com.reflection.model.Student.name
================獲取所有的字段(公共的、私有的)=================
所有字段:public java.lang.String com.reflection.model.Student.name
所有字段:private java.lang.Integer com.reflection.model.Student.age
所有字段:private java.util.Date com.reflection.model.Student.Birthday
================根據字段名獲取公共字段=================
根據字段名獲取公共字段:public java.lang.String com.reflection.model.Student.name
================根據字段名私有字段=================
根據字段名獲取公共字段:private java.lang.Integer com.reflection.model.Student.age

結論:

getFields()返回所有public的字段。

getDeclaredFields()返回所有privatepublic字段

getField()返回指定字段名public的字段。 

getDeclaredField()返回指定字段名privatepublic字段名

 

3、獲取類中的方法

在 1 中的Student類中定義幾個方法

public void method1(String str){
        System.out.println("public 修飾的方法");
    }

    private void method2(){
        System.out.println("private 修飾的方法");
    }

    String  method3(String name,Integer sex,Date age){
        System.out.println("默認修飾"+name+" "+sex+" "+age);
        return name+" "+sex+" "+age;
    }

    protected void method4(){
        System.out.println("protected 修飾的方法");
    }

通過 getMethods()   getDeclaredMethods()  getMethod()  getDeclaredMethod()這四個方法獲取各種方法

import java.lang.reflect.Method;
import java.util.Date;

public class Test06 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {

        //加載Class對象
        //會報出不存在該類的異常
        Class c=Class.forName("com.reflection.model.Student");

        //獲取所有公共方法
        System.out.println("================獲取所有公共方法=================");
        Method[] methods=c.getMethods();
        for (Method method:methods) {
            System.out.println("公共方法:"+method);
        }
        //獲取所有方法
        System.out.println("================獲取所有的方法=================");
        Method[] declaredMethods=c.getDeclaredMethods();
        for (Method declaredmethod:declaredMethods) {
            System.out.println("所有方法:"+declaredmethod);
        }

        System.out.println("================獲取特定(帶參)方法=================");
        Method method1=c.getMethod("method1",String.class);
        System.out.println("特定(帶參)方法:"+method1);

        System.out.println("================獲取特定(不帶參)方法=================");
        Method method2=c.getDeclaredMethod("method2");
        System.out.println("特定(不帶參)方法:"+method2);

        System.out.println("================獲取特定(多參)方法=================");
        Method method3=c.getDeclaredMethod("method3", String.class, Integer.class, Date.class);
        System.out.println("特定(多參)方法:"+method3);
    }
}

結果:

================獲取所有公共方法=================
公共方法:public java.lang.String com.reflection.model.Student.getName()
公共方法:public void com.reflection.model.Student.setName(java.lang.String)
公共方法:public void com.reflection.model.Student.method1(java.lang.String)
公共方法:public void com.reflection.model.Student.setSex(java.util.Date)
公共方法:public java.util.Date com.reflection.model.Student.getBirthday()
公共方法:public java.lang.Integer com.reflection.model.Student.getAge()
公共方法:public void com.reflection.model.Student.setAge(java.lang.Integer)
公共方法:public final void java.lang.Object.wait() throws java.lang.InterruptedException
公共方法:public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
公共方法:public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
公共方法:public boolean java.lang.Object.equals(java.lang.Object)
公共方法:public java.lang.String java.lang.Object.toString()
公共方法:public native int java.lang.Object.hashCode()
公共方法:public final native java.lang.Class java.lang.Object.getClass()
公共方法:public final native void java.lang.Object.notify()
公共方法:public final native void java.lang.Object.notifyAll()
================獲取所有的方法=================
所有方法:public java.lang.String com.reflection.model.Student.getName()
所有方法:public void com.reflection.model.Student.setName(java.lang.String)
所有方法:private void com.reflection.model.Student.method2()
所有方法:public void com.reflection.model.Student.method1(java.lang.String)
所有方法:java.lang.String com.reflection.model.Student.method3(java.lang.String,java.lang.Integer,java.util.Date)
所有方法:public void com.reflection.model.Student.setSex(java.util.Date)
所有方法:public java.util.Date com.reflection.model.Student.getBirthday()
所有方法:protected void com.reflection.model.Student.method4()
所有方法:public java.lang.Integer com.reflection.model.Student.getAge()
所有方法:public void com.reflection.model.Student.setAge(java.lang.Integer)
================獲取特定(帶參)方法=================
特定(帶參)方法:public void com.reflection.model.Student.method1(java.lang.String)
================獲取特定(不帶參)方法=================
特定(不帶參)方法:private void com.reflection.model.Student.method2()
================獲取特定(多參)方法=================
特定(多參)方法:java.lang.String com.reflection.model.Student.method3(java.lang.String,java.lang.Integer,java.util.Date)

結論:

getMethods()返回所有public的方法,通過結果可以看出getMethods()連父類中的public方法也可以獲取到

getDeclaredMethods()返回所有privatepublic方法名,getDeclaredMethods()獲取不到父類中的方法,只能獲取到本來中的方法

getMethod()返回指定字段名public的方法名。 

getDeclaredMethod()返回指定字方法名privatepublic字段名

 

注:

getMethods()和getDeclaredMethods()方法我們推測獲取字段的方法和獲取構造函數的方法

應該和getMethods()和getDeclaredMethods()一樣,

為了驗證getFields()和getDeclaredFields(),又定義了一個People類,

public class People {
    public String head;
    public String foot;
}

Student繼承People

================獲取所有公共字段=================
公共字段:public java.lang.String com.reflection.model.Student.name
公共字段:public java.lang.String com.reflection.model.People.head
公共字段:public java.lang.String com.reflection.model.People.foot
================獲取所有的字段(公共的、私有的)=================
所有字段:public java.lang.String com.reflection.model.Student.name
所有字段:private java.lang.Integer com.reflection.model.Student.age
所有字段:private java.util.Date com.reflection.model.Student.Birthday

getFields()可以獲取到父類的所有公共的字段,getDeclaredFields()只能獲取到本類中的字段

 

================獲取所有公共的構造方法=================
公共的構造方法:public com.reflection.model.Student(java.lang.String,java.lang.String)
================獲取所有的構造方法=================
所有構造方法:public com.reflection.model.Student(java.lang.String,java.lang.String)

上面的結果也證明了

getConstructors()不能獲取到父類的構造方法,getDeclaredConstructors()也只能獲取到本類中的構造方法

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM