对Class类的理解:(再参考资料看看)
Class类是类的抽象,通过Class类可以获得该类的构造器、属性和方法信息,并可以构造新的对象
Java 反射机制:(再参考资料看看)
通过一个对象获得该对象所对应类的信息(利用Class对象实现)
Class类的getMethod方法:
public Method getMethod(String name,
Class<?>... parameterTypes)
throws NoSuchMethodException,
SecurityException
Method类的invoke方法:
public Object invoke(Object obj, Object... args)
throws IllegalAccessException,
IllegalArgumentException,
InvocationTargetException
典型示例代码:
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
/**
* <p>Title: java反射中Method类invoke方法的用法</p> *
* <p>Description: </p> *
* <p>Copyright: Copyright (c) 2009</p> *
* <p>Company: 嘉盈公司</p> *
* @author zsw
* @version 1.0
*/
public class InvokeTester {
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public InvokeTester() {
}
public int add(int param1, int param2) {
return param1 + param2;
}
public String echo(String mesg) {
return "echo" + mesg;
}
public static void main(String[] args) {
Class classType = InvokeTester.class;
try {
Object invokertester = classType.newInstance(); //1
Method addMethod = classType.getMethod("add", new Class[] { //2
int.class, int.class
});
Object result = addMethod.invoke(invokertester, new Object[] { //3
new Integer(100), new Integer(200)
});
System.out.println(result);
//
Method echo = classType.getMethod("echo", new Class[] {String.class});
Object obj = echo.invoke(invokertester,
new Object[] {new String("jy is very good!!!")});
System.out.println(obj.toString());
InvokeTester test = new InvokeTester(); //1
test.setName("小红"); //2
//Method[] methods;
Method[] methods = test.getClass().getDeclaredMethods(); //3
//循环查找获取id方法,并执行查看是否有返回值
for (int i = 0; i < methods.length; i++) {
//如果此方法有get和Id关键字则执行
if (methods[i].getName().indexOf("get") != -1 && methods[i].getName().indexOf("Name") != -1) {
try{
// 获取此get方法返回值,判断是否有值,如果没有值说明即将执行的操作新增
if (methods[i].invoke(test, null) == null) { //4
System.out.println("此对象没有值!!!");
} else {
Object strName = methods[i].invoke(test, null);
System.out.println(strName);
}
}catch(Exception e){
System.out.print("");
}
}
}
} catch (IllegalAccessException ex) {
} catch (InstantiationException ex) {
} catch (SecurityException ex) {
} catch (NoSuchMethodException ex) {
} catch (InvocationTargetException ex) {
} catch (IllegalArgumentException ex) {
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sz_bdqn/archive/2009/12/09/4972862.aspx
参考:
http://fengqx.iteye.com/blog/413715
http://www.iteye.com/wiki/topic/413715
http://www.360doc.com/content/05/1116/12/73_31173.shtml