java课堂练习,反射

来源:互联网 发布:如何优化兵团经济结构 编辑:程序博客网 时间:2024/05/16 19:14

这是我们练习反射的作业,在此记录一下

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 classpackage 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();    }}}
0 0