java反射问题 高手来 题目: 听一个有经验的同学告诉我,在JAVA中,把一个对象看做一个方法的参数,这样理解会对以后学习反射有帮助,请高手作答。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 method.invoke(...) 他是在说这个方法吗 ? method.invoke(owner, args):执行该Method,invoke方法的参数是执行这个方法的对象,和参数数组。返回值是Object,也既是该方法的返回值。 不是吧,invoke是这样的吗? LZ朋友的话真够奇怪的,没听过反射还需要这么理解的 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(); } }看看执行结果,跟一下过程 你就知道了反射可以用来干什么了。 参数还可以是类摸板呢。利用反射就可以得到类的实例自己多多体会吧, 在写struts框架的时候可以用哈 我前一段时间用JAVA写自动化测试脚本,用到了大量的反射技术,我的理解是:通过一个类的Class对象可以了解一个类中有哪些域和方法,还可以回溯到根类,了解其根类的域和方法,你可以不必知道一个对象的方法名就可以调用其方法,很神奇的东西!甚至可以修改private属性,《JAVA编程思想》中介绍了如何利用反射去修改private属性,看来private也是不保险的哦,final好像是最保险的,即使利用反射也不能修改它。 itext 生成PDF文件中加入折线统计图 List和ArrayListd的问题 请教日历控件怎么写 数组问题 !!这是什么意思啊,大家来看看 这个计算器的小程序要怎么解释? 何为flush() BoxLayout的构造器为什么这样设计? select语句的问题 究竟整合Apache和Tomcat需要做什么工作? 亲手请教,高手请进(关于异常处理的)!!! JSplitPane和JTabel 问题
不是吧,invoke是这样的吗? LZ朋友的话真够奇怪的,没听过反射还需要这么理解的
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();
}
}看看执行结果,跟一下过程 你就知道了反射可以用来干什么了。
利用反射就可以得到类的实例
自己多多体会吧, 在写struts框架的时候可以用哈
你可以不必知道一个对象的方法名就可以调用其方法,很神奇的东西!甚至可以修改private属性,《JAVA编程思想》中介绍了如何利用反射去修改private属性,看来private也是不保险的哦,final好像是最保险的,即使利用反射也不能修改它。