Java reflection


/*
My understanding on this is that JAVA REFELECTION is a container or an operator for a Class.
*/



//package reference
import java.lang.reflect.*;



//List the method(s) of a class.
...

Class c = Class.forName("ClassName");//load the class' structure
Method m[] = c.getDeclaredMethods();
for(int i = 0;i < m.length; i++){
System.out.println(m[i].getName());
}

...

//List the parameters TYPE of a method
...
Class pretype[] = m.getParameterTypes();

//Invoke a specified method
...
Class c = Class.forName("ClassName");
Class params[] = new Class[2];//2 is the operands' count.
Method m = c.getMethod("appropriateMethodName",params);
//if there is no argument in the method, need throw an exception.
ClassName cn = new ClassName();
Object args[] = new Object[2];//same with the previous CLASS.
m.invoke(cn,args);
//if there's a return value,should use a Object to receive it.like
// Object o = m.invoke(cn,args);
// then convert the type.like
// Integer i = (Integer)o;



for details:
[url]http://java.sun.com/developer/technicalArticles/ALT/Reflection/[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值