Java之反射--練習

Java仗劍走天涯發表於2017-05-05

定義Student 類:包含:姓名和年齡等屬性,有參和無參構造方法,輸出所有資訊的方法

1.使用多種方法生成一個Student類的Class物件
2.使用Class類獲取Student類的結構資訊並輸出
3.通過有參(無參)構造方法動態建立Student類的物件
4.使用反射修改和查詢Student類的姓名屬性
5.使用反射動態執行Student類輸出資訊的方法


程式碼:

Student類

package 敲程式碼;
//定義Student 類,包含:姓名和年齡等屬性,有參和無參構造方法,輸出所有資訊的方法


public class Student {
    private String name;
    private int age;
    private String sex;

    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
    }
    public Student(){}
    public Student(String name,int age,String sex){
        this.name=name;
        this.age=age;
        this.sex=sex;
    }
    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 String getSex() {
        return sex;
    }

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

    public void eat(){
        System.out.println("eat apple!");
    }

    private void run(){
        System.out.println("run and listen to music!");
    }

    public static void play(){
        System.out.println("play game");
    }

}

測試類:

package 敲程式碼;

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


public class Test {
    public static void main(String[] args) {
        //1.獲取類物件
        //first
        Student student=new Student();
        Class clazz=student.getClass();
        System.out.println(clazz.hashCode());
        //second
        Class clazz2=Student.class;
        System.out.println(clazz2.hashCode());
        //third
        try {
            Class clazz3=Class.forName("敲程式碼.Student");
            System.out.println(clazz3.hashCode());
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        //2.使用Class類獲取Student類的結構資訊並輸出
        try {
            //構造方法
            Constructor[] constructors=clazz.getDeclaredConstructors();
            for (Constructor constructor2 : constructors) {
                System.out.println(constructor2);
            }

            //獲取方法物件
            Method[] methods=clazz.getDeclaredMethods();
            for (Method method : methods) {
                System.out.println(method);
            }

            //獲取屬性
            Field[] fields=clazz.getDeclaredFields();
            for (Field field : fields) {
                System.out.println(field);
            }
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //3.通過有參(無參)構造方法動態建立Student類的物件
        try {
            Constructor<Student> constructor=clazz2.getConstructor(String.class,int.class,String.class);
            Student student2=constructor.newInstance("張三",18,"張三");
            System.out.println(student2);


            //4.使用反射修改和查詢Student類的姓名屬性
            student2.setName("李四");
            System.out.println(student2.getName());

            //5.使用反射動態執行Student類輸出資訊的方法
            Method method=clazz.getMethod("eat", null);
            method.invoke(student, null);
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }
}

執行結果:

這裡寫圖片描述
這裡寫圖片描述

相關文章