Java之反射--练习
生活随笔
收集整理的這篇文章主要介紹了
Java之反射--练习
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
定義Student 類:包含:姓名和年齡等屬性,有參和無參構造方法,輸出所有信息的方法
1.使用多種方法生成一個Student類的Class對象
2.使用Class類獲取Student類的結構信息并輸出
3.通過有參(無參)構造方法動態(tài)創(chuàng)建Student類的對象
4.使用反射修改和查詢Student類的姓名屬性
5.使用反射動態(tài)執(zhí)行Student類輸出信息的方法
代碼:
Student類
package 敲代碼; //定義Student 類,包含:姓名和年齡等屬性,有參和無參構造方法,輸出所有信息的方法public class Student {private String name;private int age;private String sex;@Overridepublic 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.獲取類對象//firstStudent student=new Student();Class clazz=student.getClass();System.out.println(clazz.hashCode());//secondClass clazz2=Student.class;System.out.println(clazz2.hashCode());//thirdtry {Class clazz3=Class.forName("敲代碼.Student");System.out.println(clazz3.hashCode());} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();}//3.通過有參(無參)構造方法動態(tài)創(chuàng)建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.使用反射動態(tài)執(zhí)行Student類輸出信息的方法Method method=clazz.getMethod("eat", null);method.invoke(student, null);} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();} } }運行結果:
總結
以上是生活随笔為你收集整理的Java之反射--练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java之反射代码演示说明
- 下一篇: JavaScript:window.on