package com.wanggc.reflection; import java.lang.reflect.Method; /** * Java 反射练习。 * * @author Wanggc */ public class ForNameTest { /** * 入口函数。 * * @param args * 参数 * @throws Exception * 错误信息 */ public static void main(String[] args) throws Exception { // 获得Class Class<?> cls = Class.forName(args[0]); // 通过Class获得所对应对象的方法 Method[] methods = cls.getMethods(); // 输出每个方法名 for (Method method : methods) { System.out.println(method); } } }
1 package com.wanggc.reflection; 2 3 import java.lang.reflect.Method; 4 5 /** 6 * Java 反射练习。 7 * 8 * @author Wanggc 9 */ 10 public class ReflectionTest { 11 public static void main(String[] args) throws Exception { 12 DisPlay disPlay = new DisPlay(); 13 // 获得Class 14 Class<?> cls = disPlay.getClass(); 15 // 通过Class获得DisPlay类的show方法 16 Method method = cls.getMethod("show", String.class); 17 // 调用show方法 18 method.invoke(disPlay, "Wanggc"); 19 } 20 } 21 22 class DisPlay { 23 public void show(String name) { 24 System.out.println("Hello :" + name); 25 } 26 } 前面说过,Java程序的每个类都会有个Class对象与之对应。Java反射的第一步就是获得这个Class对象,如代码14行。当然,每个类的方法也必有一个Method对象与之对应。要通过反射的方式调用这个方法,就要首先获得这个方法的Method对象,如代码16行,然后用Method对象反过来调用这个方法,如代码18行。注意16行getMethod方法的第一个参数是方法名,第二个是此方法的参数类型,如果是多个参数,接着添加参数就可以了,因为getMethod是可变参数方法。执行18行代码的invoke方法,其实也就是执行show方法,注意invoke的第一个参数,是DisPlay类的一个对象,也就是调用DisPlay类哪个对象的show方法,第二个参数是给show方法传递的参数。类型和个数一定要与16行的getMethod方法一直。 上例讲述了如何通过反射调用某个类的方法,下面将再通过一个实例讲述如何通过反射给某个类的属性赋值: 1 package com.wanggc.reflection; 2 3 import java.lang.reflect.Field; 4 5 /** 6 * Java 反射之属性练习。 7 * 8 * @author Wanggc 9 */ 10 public class ReflectionTest { 11 public static void main(String[] args) throws Exception { 12 // 建立学生对象 13 Student student = new Student(); 14 // 为学生对象赋值 15 student.setStuName("Wanggc"); 16 student.setStuAge(24); 17 // 建立拷贝目标对象 18 Student destStudent = new Student(); 19 // 拷贝学生对象 20 copyBean(student, destStudent); 21 // 输出拷贝结果 22 System.out.println(destStudent.getStuName() + ":" 23 + destStudent.getStuAge()); 24 } 25 26 /** 27 * 拷贝学生对象信息。 28 * 29 * @param from 30 * 拷贝源对象 31 * @param dest 32 * 拷贝目标对象 33 * @throws Exception 34 * 例外 35 */ 36 private static void copyBean(Object from, Object dest) throws Exception { 37 // 取得拷贝源对象的Class对象 38 Class<?> fromClass = from.getClass(); 39 // 取得拷贝源对象的属性列表 40 Field[] fromFields = fromClass.getDeclaredFields(); 41 // 取得拷贝目标对象的Class对象 42 Class<?> destClass = dest.getClass(); 43 Field destField = null; 44 for (Field fromField : fromFields) { 45 // 取得拷贝源对象的属性名字 46 String name = fromField.getName(); 47 // 取得拷贝目标对象的相同名称的属性 48 destField = destClass.getDeclaredField(name); 49 // 设置属性的可访问性 50 fromField.setAccessible(true); 51 destField.setAccessible(true); 52 // 将拷贝源对象的属性的值赋给拷贝目标对象相应的属性 53 destField.set(dest, fromField.get(from)); 54 } 55 } 56 } 57 58 /** 59 * 学生类。 60 */ 61 class Student { 62 /** 姓名 */ 63 private String stuName; 64 /** 年龄 */ 65 private int stuAge; 66 67 /** 68 * 获取学生姓名。 69 * 70 * @return 学生姓名 71 */ 72 public String getStuName() { 73 return stuName; 74 } 75 76 /** 77 * 设置学生姓名 78 * 79 * @param stuName 80 * 学生姓名 81 */ 82 public void setStuName(String stuName) { 83 this.stuName = stuName; 84 } 85 86 /** 87 * 获取学生年龄 88 * 89 * @return 学生年龄 90 */ 91 public int getStuAge() { 92 return stuAge; 93 } 94 95 /** 96 * 设置学生年龄 97 * 98 * @param stuAge 99 * 学生年龄 100 */ 101 public void setStuAge(int stuAge) { 102 this.stuAge = stuAge; 103 } 104 }
package com.wanggc.reflection; 2 3 import java.lang.reflect.Field; 4 5 /** 6 * Java 反射之属性练习。 7 * 8 * @author Wanggc 9 */ 10 public class ReflectionTest { 11 public static void main(String[] args) throws Exception { 12 // 建立学生对象 13 Student student = new Student(); 14 // 为学生对象赋值 15 student.setStuName("Wanggc"); 16 student.setStuAge(24); 17 // 建立拷贝目标对象 18 Student destStudent = (Student) copyBean(student); 19 // 输出拷贝结果 20 System.out.println(destStudent.getStuName() + ":" 21 + destStudent.getStuAge()); 22 } 23 24 /** 25 * 拷贝学生对象信息。 26 * 27 * @param from 28 * 拷贝源对象 29 * @param dest 30 * 拷贝目标对象 31 * @throws Exception 32 * 例外 33 */ 34 private static Object copyBean(Object from) throws Exception { 35 // 取得拷贝源对象的Class对象 36 Class<?> fromClass = from.getClass(); 37 // 取得拷贝源对象的属性列表 38 Field[] fromFields = fromClass.getDeclaredFields(); 39 // 取得拷贝目标对象的Class对象 40 Object ints = fromClass.newInstance(); 41 for (Field fromField : fromFields) { 42 // 设置属性的可访问性 43 fromField.setAccessible(true); 44 // 将拷贝源对象的属性的值赋给拷贝目标对象相应的属性 45 fromField.set(ints, fromField.get(from)); 46 } 47 48 return ints; 49 } 50 } 51 52 /** 53 * 学生类。 54 */ 55 class Student { 56 /** 姓名 */ 57 private String stuName; 58 /** 年龄 */ 59 private int stuAge; 60 61 /** 62 * 获取学生姓名。 63 * 64 * @return 学生姓名 65 */ 66 public String getStuName() { 67 return stuName; 68 } 69 70 /** 71 * 设置学生姓名 72 * 73 * @param stuName 74 * 学生姓名 75 */ 76 public void setStuName(String stuName) { 77 this.stuName = stuName; 78 } 79 80 /** 81 * 获取学生年龄 82 * 83 * @return 学生年龄 84 */ 85 public int getStuAge() { 86 return stuAge; 87 } 88 89 /** 90 * 设置学生年龄 91 * 92 * @param stuAge 93 * 学生年龄 94 */ 95 public void setStuAge(int stuAge) { 96 this.stuAge = stuAge; 97 } 98 }
原文:https://www.cnblogs.com/yujiantong/p/15226969.html