java反射机制初步认识<二>获取并执行方法

来源:互联网 发布:qq for mac 10.8.5 编辑:程序博客网 时间:2024/05/24 04:18

上一篇文章简要的介绍了常用的通过反射获取成员变量的一些方法,这篇文章,我们来简要的介绍一些常用的通过反射来获取成员方法并执行的手段。废话不多说,直接看代码:

      <一>.定义一个实体类Student:

package test;import java.util.Date;public class Student {/** 学号 */private long id;/** 姓名 */private String name;/** 年龄 */private int age;/** 出生年月 */private Date birthDay;public Student() {}public Student(long id, String name, int age, Date birthDay) {this.id = id;this.name = name;this.age = age;this.birthDay = birthDay;}public long getId() {return id;}public void setId(long id) {this.id = id;}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 Date getBirthDay() {return birthDay;}public void setBirthDay(Date birthDay) {this.birthDay = birthDay;}}

<二>定义一个单例的实体管理对象:

package test;import java.util.HashMap;import java.util.Map;public class StudentManager {private static StudentManager instance = new StudentManager();private Map<Long, Student> map = new HashMap<>();private StudentManager() {}public static StudentManager getInstance() {return instance;}public void addStudent(Student student) {map.put(student.getId(), student);System.out.println("添加学生:" + student.getName() + ",目前共有学生:"+ map.size());}public void deleteStudent(Student student) {map.remove(student.getId());System.out.println("删除学生:" + student.getName() + ",目前共有学生:"+ map.size());}public Map<Long, Student> getMap() {return map;}public void setMap(Map<Long, Student> map) {this.map = map;}}




<三>.反射演示:

package test;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.Type;import java.util.Date;public class Test {public static void main(String[] args) throws NoSuchMethodException,SecurityException, IllegalAccessException,IllegalArgumentException, InvocationTargetException,InstantiationException {StudentManager sm = StudentManager.getInstance();Class<? extends Object> clazz = sm.getClass();// 1.获取StudentManager中所有的方法,包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法Method[] singleMethods = clazz.getDeclaredMethods();// 2.获取StudentManager中所有的方法,包括由该类或接口声明 ,以及从超类和超接口继承的那些的类或接口的公共方法Method[] allMethods = clazz.getMethods();// 3.获取指定名称的方法,第二个参数为该方法参数的class对象(按顺序)Method assignMethod = clazz.getDeclaredMethod("addStudent",Student.class);// 4.获取方法返回类型Type paramType = assignMethod.getGenericReturnType();// 5.以Type数组获取方法所有形参的类型(按照声明顺序)Type[] parameterTypes1 = assignMethod.getGenericParameterTypes();for (Type type : parameterTypes1) {System.out.println(type.toString());}// 6.以Class数组获取方法中所有的形参类型(按照声明顺序)Class<? extends Object>[] parameterTypes = assignMethod.getParameterTypes();for (Class<? extends Object> temp : parameterTypes) {System.out.println(temp.getSimpleName());}// 注意:在java1.7(含)以前,无法通过反射获取方法的形参名称,若想获取,可以采用注解。java1.8可以支持获取形参名称Student studenta = new Student(10012L, "张三", 21, new Date());Student studentb = new Student(10013L, "李四", 22, new Date());// 7.调用添加方法assignMethod.invoke(sm, studenta);assignMethod.invoke(sm, studentb);// 8.调用删除方法Method deleteMethod = clazz.getDeclaredMethod("deleteStudent",Student.class);deleteMethod.invoke(sm, studentb);}}


运行结果:

class test.Student
Student
添加学生:张三,目前共有学生:1
添加学生:李四,目前共有学生:2
删除学生:李四,目前共有学生:1



0 0