1. 程式人生 > >反射相關

反射相關

1:反射獲取Class類的三種方法

  • 通過Class的靜態方法獲取:Class class = Class.forName(“類的路徑:包名.類名”)
  • 通過執行時類的物件獲取:Person p = new Person();  Class clazz = p.getClass();
  • 呼叫執行時類本身的.class屬性:Class class = String.class; //String類的類型別 (String類的位元組碼)

 2:反射獲取類名、構造方法、方法和域

建立一個類:

package reflect;

/**
 * @ClassName Student
 * @Description TODO
 * @Author Kikityer
 * @Date 2018/11/7 11:10
 * @Version 1.0.0
 **/
public class Student {

    private String namee;

    private Integer age;

    private String grage;

    private Integer studentNum;

    public Student() {
    }

    public Student(String namee) {
        this.namee = namee;
    }

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

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

    public Student(String namee, Integer age, String grage, Integer studentNum) {
        this.namee = namee;
        this.age = age;
        this.grage = grage;
        this.studentNum = studentNum;
    }

    protected void study(){

    }

    public String getNamee() {
        return namee;
    }

    public void setNamee(String namee) {
        this.namee = namee;
    }

    public Integer getAge() {
        return age;
    }

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

    public String getGrage() {
        return grage;
    }

    public void setGrage(String grage) {
        this.grage = grage;
    }

    public Integer getStudentNum() {
        return studentNum;
    }

    public void setStudentNum(Integer studentNum) {
        this.studentNum = studentNum;
    }
}

測試類:

package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @ClassName ReflectTest
 * @Description TODO
 * @Author Kikityer
 * @Date 2018/11/7 11:15
 * @Version 1.0.0
 **/
public class ReflectTest {
    public static void main(String[] args) {

        Class aClass;

        {
            try {
                aClass = Class.forName("reflect.Student");
                System.out.println("類的完整類名為:"+ aClass.toString());
                System.out.println("類的類名為:"+ aClass.getSimpleName());
                System.out.println("------------------------------");
                Constructor[] constructors = aClass.getDeclaredConstructors();
                for (Constructor cons : constructors){
                    System.out.println(cons.getName());
                    System.out.println(cons.getModifiers());
                    /**
                     * getModifiers()方法返回int型別值表示該欄位的修飾符。
                     * PUBLIC: 1
                     * PRIVATE: 2
                     * PROTECTED: 4
                     * STATIC: 8
                     * FINAL: 16
                     * SYNCHRONIZED: 32
                     * VOLATILE: 64
                     * TRANSIENT: 128
                     * NATIVE: 256
                     * INTERFACE: 512
                     * ABSTRACT: 1024
                     * STRICT: 2048
                     */
                }
                Constructor[] constructors1 = aClass.getConstructors();
                for (Constructor cons1 : constructors1){
                    System.out.println(cons1.getName());
                    System.out.println(cons1.getModifiers());
                }
                System.out.println("------------------------------");
                Method[] methods = aClass.getDeclaredMethods();
                for (Method method : methods){
                    System.out.println(method.getName());
                    System.out.println(method.getModifiers());
                }
                System.out.println("------------------------------");
                Field[] fields = aClass.getDeclaredFields();
                for (Field field: fields){
                    System.out.println(field.getName());
                    System.out.println(field.getModifiers());
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}