java課堂練習,反射

這是我們練習反射的作業,在此記錄一下

TeacherFunction項目,導出作爲jar包

package function.teacher;

public class TeacherFunction {
private String TName;
private int TAge;

public TeacherFunction() {
    super();
    // TODO Auto-generated constructor stub
}
@Deprecated 
public void curriculaVariable(){
    System.out.println("Teacher curricula-variable");
}
public void teaching(){
    System.out.println("Teachers in class");
}

}

導入第一個項目jar包到另外的項目,我命名爲WorkClient

定義了兩個類,ProfessorClient類,繼承TeacherFunction重寫了teaching方法,Client類用來動態創建TeacherFunction類,查看修改屬性等

ProfessorClient class
package work.client;
import function.teacher.*;
public class ProfessorClient extends TeacherFunction{
public void teaching(){
    System.out.println("professor in class");
}

}

Client class

package work.client;



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

import function.teacher.TeacherFunction;

public class Client {

public static void main(String[] args) {        
    //使用多種方法生成一個教師類的class對象
    //method one
    TeacherFunction tf = new TeacherFunction();
    Class clazz = tf.getClass();
    try {
        Object obj = clazz.newInstance();
    } catch (InstantiationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IllegalAccessException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    //method two
    try {
        Class clazz1 = Class.forName("function.teacher.TeacherFunction");
        Object obj1 = clazz.newInstance();
    } catch (ClassNotFoundException 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();
    }
    //Method three
    Class clazz2 = TeacherFunction.class;
    try {
        Object obj2 = clazz2.newInstance();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //使用Class類獲取教師類的結構信息並輸出
    //字段名&類型
    Field fields[] = clazz2.getDeclaredFields();
    for(Field field:fields){
        System.out.println(field.getName()+","+field.getType());
    }
    //方法
    Method methods[] = clazz2.getDeclaredMethods();
    for(Method method:methods){
        System.out.println(method.getName());;
    }
    //構造方法
    Constructor constractors[] = clazz2.getDeclaredConstructors();
    for(Constructor constructor:constractors){
        System.out.println(constructor);
    }
    //包名
    System.out.println(clazz2.getPackage().getName());
    //完整名字
    System.out.println(clazz2.getName());
    //父類
//      System.out.println(clazz2.getSuperclass());
    //通過無參構造方法,使用反射技術動態創建教師類對象
    try {
        TeacherFunction teafun = (TeacherFunction)clazz.newInstance();
        System.out.println(teafun);
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //是用反射修改和查詢教師類的的教齡屬性
    try {

            Class clazz3 = Class.forName("function.teacher.TeacherFunction");
        Object obj3 = clazz3.newInstance();
        Field f = clazz2.getDeclaredField("TAge");
        f.setAccessible(true);
        //查詢
        System.out.println(f.get(obj3));
        //修改
        f.set(obj3, 18);
        System.out.println(f.get(obj3));
        //使用反射動態執行教師類的選課方法


        Method cv = clazz3.getDeclaredMethod("curriculaVariable", null);
        Object result = cv.invoke(obj3, null);
    } catch (NoSuchFieldException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
    catch (ClassNotFoundException 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 (NoSuchMethodException 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();
    }


}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章