java反射问题 高手来

zhls00000 2009-01-17 05:19:13
题目:
听一个有经验的同学告诉我,在JAVA中,把一个对象看做一个方法的参数,这样理解会对以后学习反射有帮助,请高手作答。
...全文
249 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
zx8813443 2009-02-06
  • 打赏
  • 举报
回复
我前一段时间用JAVA写自动化测试脚本,用到了大量的反射技术,我的理解是:通过一个类的Class对象可以了解一个类中有哪些域和方法,还可以回溯到根类,了解其根类的域和方法,
你可以不必知道一个对象的方法名就可以调用其方法,很神奇的东西!甚至可以修改private属性,《JAVA编程思想》中介绍了如何利用反射去修改private属性,看来private也是不保险的哦,final好像是最保险的,即使利用反射也不能修改它。
liang__ 2009-02-06
  • 打赏
  • 举报
回复
反射还能这样理解!!!
JamesLiu 2009-02-01
  • 打赏
  • 举报
回复
反射有这样学吗
wang663632304 2009-01-29
  • 打赏
  • 举报
回复
参数还可以是类摸板呢。
利用反射就可以得到类的实例
自己多多体会吧, 在写struts框架的时候可以用哈
龙龙ago 2009-01-29
  • 打赏
  • 举报
回复
我目前只知道反射是可以获取对象的版本号以及相关信息的,感觉反射的用法有一点点像枚举,
呵呵!
rainsilence 2009-01-22
  • 打赏
  • 举报
回复
有经验?????
参数?????
跟反射有什么关系????
我是小烨 2009-01-22
  • 打赏
  • 举报
回复
反射的作用太多了...如果你只是刚学,没有必要去深究,因为反射不在于代码有多难,而是在哪种场合适用...
kukufly 2009-01-22
  • 打赏
  • 举报
回复
应该先弄明白 反射 说的是什么!
henry_fuzr 2009-01-22
  • 打赏
  • 举报
回复
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.RefReal

package com.learn.reflection;

public class A {

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

}

看看执行结果,跟一下过程 你就知道了反射可以用来干什么了。
pauliuyou 2009-01-21
  • 打赏
  • 举报
回复
意思是不是方法的参数可以是对象呢? 比较迷糊
foxandrose 2009-01-20
  • 打赏
  • 举报
回复
学习反射很简单!

困难的是什么时候去用!
zhls00000 2009-01-20
  • 打赏
  • 举报
回复
可是把把一个对象看做一个方法的参数怎么有点像面相过程变成呢 我疑问就在这 请指教
dehua007 2009-01-20
  • 打赏
  • 举报
回复

怎么理解都可以,
吸收了就好。
一洽客服系统 2009-01-18
  • 打赏
  • 举报
回复
是你朋友还没理解好反射吧 ~
dudenglan 2009-01-18
  • 打赏
  • 举报
回复
反射里只有几个方法,但每个方法都很实用,可以将一个类的所有东西全部显示出来!
gongfuliang 2009-01-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pxcong007 的回复:]
method.invoke(...)

他是在说这个方法吗 ?
[/Quote]

不是吧,invoke是这样的吗? LZ朋友的话真够奇怪的,没听过反射还需要这么理解的
bzwm 2009-01-17
  • 打赏
  • 举报
回复
等一下啊,,,,我慢慢理解下。。。。
zhls00000 2009-01-17
  • 打赏
  • 举报
回复
那如果执行别的方法,这样理解好吗?
lord_is_layuping 2009-01-17
  • 打赏
  • 举报
回复
method.invoke(owner, args):执行该Method,invoke方法的参数是执行这个方法的对象,和参数数组。返回值是Object,也既是该方法的返回值。
ZangXT 2009-01-17
  • 打赏
  • 举报
回复
好奇怪的话。
加载更多回复(1)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧