目录
Method
概念:
使用 Java 的反射机制获得的指定类中指定方法的对象;
通过反射调用类的私有方法时,要先在这个私有方法对应的 Method 对象上调用 setAccessible(true)(暴力访问) 来取消对这个方法的访问检查,再调用 invoke() 方法来执行这个私有方法;
概念的来源操作成员方法:
主要方法:
1.Method getMethod(String name,Class...args);
根据方法名和参数类型获得对应的方法对象,只能获得public的
2.Method getDeclaredMethod(String name,Class...args);
根据方法名和参数类型获得对应的方法对象,包括非public的【只能获取本类中的一个】
3.Method[] getMethods();
获得类中的所有成员方法对象,返回数组,只能获得public修饰的且包含父类的
4.Method[] getDeclaredMethods();
获得类中的所有成员方法对象,返回数组,只获得本类的,包括非public的方法【只能获取本类中所有】
代码演示:
//新建一个student对象,在里面定义很多方法(定义包含静态方法,非静态方法,及4中权限的方法) public class Student { public static void jingTai(){ System.out.println("静态方法"); } public void feiJingTai(){ System.out.println("普通方法"); } private void siYou(){ System.out.println("私有方法"); } protected void baoHu(){ System.out.println("保护方法"); } void moRen(){ System.out.println("默认的方法"); } } //对于上面四种方法的使用的代码(我自己的理解) public class MethodTest { public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { //获得字节码对象 Class<Student> studentClass = Student.class; //获取单个方法,获取public的方法 Method jingTai = studentClass.getMethod("jingTai"); System.out.println(jingTai);//public static void Methodtest.Student.jingTai() //通过发射机制执行成员方法 Student student = new Student(); Object invoke = jingTai.invoke(student,null); System.out.println(invoke);//静态方法 null //因为getMethod只能获取public //Method baoHu = studentClass.getMethod("baoHu");//报错,NoSuchMethodException 不能匹配该方法异常 //获取保护方法,getDeclaredMethod,只能获取一个方法,但是权限不限 Method baoHu = studentClass.getDeclaredMethod("baoHu"); System.out.println(baoHu);//protected void Methodtest.Student.baoHu() //获取多个方法,public,包括父类型中的public方法 Method[] methods = studentClass.getMethods(); //遍历 for (Method method : methods) { System.out.println(method); } //获得类中的所有成员方法对象,返回数组,只获得本类的,包括非public的方法【只能获取本类中所有】 Method[] declaredMethods = studentClass.getDeclaredMethods(); for (Method declaredMethod : declaredMethods) { System.out.println(declaredMethod); } } }
结果:
反射简易框架模拟
设计一个框架,可以运行任何类型(如很多个类)的任何方法。要执行的类和方法在配置文件中指定。
当要切换类的方法调用的时候,我们只要改变配置文件就可以了
大概步骤:
1.先定义两个类,在类里面写成员变量和成员方法
2.再在src文件里新建一个配置文件config.properties.在里面配置指定路径(配置类名className和方法名methodName)
如:
className=Kuanjia.Student//这里复制类所在包下的名字.类名 methodName=study//成员方法名3.在test类里面
1.加载配置文件中的信息 2.通过类加载器加载3.创建类的对象4.执行方法
代码:
//配置文件 className=Kuanjia.Student methodName=study //只要改类名和方法名就可以随时调用类里面的成员方法 //Person类 public class Person { String naem; int age; public void dong(){ System.out.println("person干活方法"); } } //Student类 public class Student { String name; int age; public void study(){ System.out.println("student学习方法"); } } //Test类 public class Test { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { //加载配置文件中的信息 ResourceBundle bundle = ResourceBundle.getBundle("config"); String className = bundle.getString("className"); String methodName = bundle.getString("methodName"); //通过类加载器加载 Class<?> aClass = Class.forName(className); //创建类的对象 Object obj = aClass.newInstance(); //执行方法 Method method = aClass.getMethod(methodName); method.invoke(obj);//student学习方法 } }