题目:
  听一个有经验的同学告诉我,在JAVA中,把一个对象看做一个方法的参数,这样理解会对以后学习反射有帮助,请高手作答。

解决方案 »

  1.   

    method.invoke(...) 他是在说这个方法吗 ?
      

  2.   

    method.invoke(owner, args):执行该Method,invoke方法的参数是执行这个方法的对象,和参数数组。返回值是Object,也既是该方法的返回值。
      

  3.   


    不是吧,invoke是这样的吗?  LZ朋友的话真够奇怪的,没听过反射还需要这么理解的
      

  4.   

    package com.learn.reflection;public class RefReal {
        private int intReal;
        private String strReal;
        public int getIntReal() {
            System.out.println(intReal);
            return intReal;
        }
        public void setIntReal(int intReal) {
            System.out.println("Method :setIntReal "+intReal);
            this.intReal = intReal;
        }
        public String getStrReal() {
            return strReal;
        }
        public void setStrReal(String strReal) {
            System.out.println("Method :setStrReal "+strReal);
            this.strReal = strReal;
        }
        
        public void otherMethod(int i,String strReal){
            System.out.println("Method: otherMethod "+ i+strReal);
        }
    }
    package com.learn.reflection;import java.io.IOException;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.Properties;public class ReflectionTest {
        private String proPath = "reflectionClass.properties" ;
        @SuppressWarnings("unchecked")
        public void reftest(){
            //read form properties file
            java.io.InputStream is = ReflectionTest.class.getClassLoader().getResourceAsStream(proPath);
            Properties properties = new Properties();
            try {
                properties.load(is);
                String className = properties.getProperty("className");
                
                Class c = Class.forName(className);
                Object o = c.newInstance();
                Method[] methods = c.getMethods();
                for(Method method :methods){
                    
                    if(method.getName().contains("set")){
                            System.out.println("方法名称:"+method.getName()+"返回类型:"+method.getReturnType());
                            System.out.println("参数类型:"+method.getParameterTypes()[0]);
                            if(method.getParameterTypes()[0].toString().equals("int")){
                                method.invoke(o, 123);
                            }
                    }
                }
                
            } catch (IOException e) {
                System.out.println("read error");
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }reflectionClass.properties内容,放在scr下面。
    className = com.learn.reflection.RefRealpackage com.learn.reflection;public class A {

    public static void main(String[] args) {
    new ReflectionTest().reftest();
    }

    }看看执行结果,跟一下过程 你就知道了反射可以用来干什么了。
      

  5.   

    参数还可以是类摸板呢。
    利用反射就可以得到类的实例
    自己多多体会吧, 在写struts框架的时候可以用哈
      

  6.   

    我前一段时间用JAVA写自动化测试脚本,用到了大量的反射技术,我的理解是:通过一个类的Class对象可以了解一个类中有哪些域和方法,还可以回溯到根类,了解其根类的域和方法,
    你可以不必知道一个对象的方法名就可以调用其方法,很神奇的东西!甚至可以修改private属性,《JAVA编程思想》中介绍了如何利用反射去修改private属性,看来private也是不保险的哦,final好像是最保险的,即使利用反射也不能修改它。