黑马程序员技术交流社区

标题: 关于反射获取Method[] [打印本页]

作者: 吴迪    时间: 2016-1-31 09:29
标题: 关于反射获取Method[]
当用反射获取方法数组,并进行遍历调用时,会遇到无法访问等问题,暴力放射也会报错误
在网上看到用BeanUtils包,基础班的新课程中没讲到它的用法,求大神指点
作者: 15266913109    时间: 2016-1-31 10:59
帮顶。。。
作者: j6819236    时间: 2016-1-31 12:18
不知到楼主是遇到什么问题了,我自己编了一遍并没发现问题,楼主最好还是把源码发出来看看。
  1. package com.heima;

  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;

  4. public class Test4 {

  5.         public static void main(String[] args) throws ReflectiveOperationException, IllegalArgumentException, InvocationTargetException {
  6.        Test_Methods tm=new Test_Methods();
  7.            Class clazz=tm.getClass();
  8.        Method[] ms=clazz.getDeclaredMethods();
  9.        for (Method m : ms) {
  10.                 m.setAccessible(true);
  11.                 System.out.println(m.invoke(tm));
  12.         }
  13.         }
  14.         
  15.    
  16. }
  17. class Test_Methods{
  18.         private String print1(){
  19.                 System.out.println("这是print1");
  20.                 return "执行print1方法完毕!";
  21.         }
  22.         public String print2(){
  23.                 System.out.println("这是print2");
  24.                 return "执行print2方法完毕!";
  25.         }
  26.         private String print3(){
  27.                 System.out.println("这是print3");
  28.                 return "执行print3方法完毕!";
  29.         }
  30. }
复制代码


作者: 吴迪    时间: 2016-1-31 13:44
j6819236 发表于 2016-1-31 12:18
不知到楼主是遇到什么问题了,我自己编了一遍并没发现问题,楼主最好还是把源码发出来看看。

...

谢谢你的回复,下段代码是获取TestMethod类的方法数组,并调用,方法会访问到对应属性,会出错
  1. package Test3;

  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;

  4. public class TestDemo {

  5.         /**
  6.          * @param args
  7.          * @throws InstantiationException
  8.          * @throws InvocationTargetException
  9.          * @throws IllegalArgumentException
  10.          * @throws IllegalAccessException
  11.          */
  12.         public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
  13.                 Class clazz = TestMethod.class;
  14.                 Method[] methods = clazz.getMethods();
  15.                 for (Method method : methods) {
  16.                         method.setAccessible(true);
  17.                         System.out.println(method.invoke(clazz.newInstance(),null));
  18.                 }
  19.         }

  20. }
  21. class TestMethod{
  22.         public String name = "zhangsan";
  23.         public int x = 1;
  24.         public String getName() {
  25.                 return name;
  26.         }
  27.         /*public void setName(String name) {
  28.                 this.name = name;
  29.         }*/
  30.         public int getX() {
  31.                 return x;
  32.         }
  33.         /*public void setX(int x) {
  34.                 this.x = x;
  35.         }*/
  36.        
  37. }
复制代码

作者: j6819236    时间: 2016-1-31 15:52
本帖最后由 j6819236 于 2016-1-31 15:54 编辑

Method[] methods = clazz.getMethods();
改成Method[] methods = clazz.getDeclaredMethods();就行了~


作者: 吴迪    时间: 2016-2-1 17:41
j6819236 发表于 2016-1-31 15:52
Method[] methods = clazz.getMethods();
改成Method[] methods = clazz.getDeclaredMethods();就行了~

关键是不知道为什么这么改呀,原理




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2