如何动态执行类中的某个方法?在线等待! Class[] cls = {null};Method method = c.getMethod("get" + getClassName,cls); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你原来的写法是查找下面这个函数public int getTestA() {。。 } let me try try :) 好像还是不行哟!刚才那个类有个地方写错了,两个get(返回String类型)方法如下:public String getTestA(String testA) { return testA; } public String getTestB(String testB) { return testB; }} (new MyTest()).getXXX("XXX(String getClassName)") Class[] cls = {String.class};Method method = c.getMethod("get" + getClassName,cls);搞错了,不好意思,这回应该可以了 回楼上,似乎好像不行,我这个getXXX(String getClassName)方法也是放在class Test{}中,会不会有影响呢? 下面这个程序我执行通过,你比较看看。应该是可以的。如果还不行,把你的全部代码贴出来import java.io.*;import java.lang.reflect.Method;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author unascribed * @version 1.0 */public class Test { private String testA; private String testB; public static void main(String args[])throws IOException{ Test t = new Test(); t.getXXX( "TestA" ); } public String getTestA(String testA) { System.out.println( testA ); return testA; } public String getTestB(String testB) { return testB; } public int getXXX(String getClassName) { int no = 0; if(getClassName!=null){ Class c = this.getClass(); Method[] methods = c.getMethods(); for(int i=0;i<methods.length;i++){ String className = methods[i].getName(); if(className.length()>3&&className.substring(0,3).equals("get")){ if(className.substring(3).equals(getClassName)){ try { Class[] cls = {String.class}; Method method = c.getMethod("get" + getClassName,cls); method.invoke(this, new Object[]{"hehe"}); }catch (Exception ex) { System.out.println(" get"+getClassName+" 'not found "); ex.printStackTrace(); } } } } } return no; }} 首先谢谢你的一直的关注,我试了试你的方法,为什么当Test t = new Test();t.getXXX( "TestC" );//Test类没有TestC方法并不抛出异常呢?你试试看 if(className.substring(3).equals(getClassName)){你自己判断掉了,根本就没有执行到try这里这也是为什么我已开始看错了其实你判断后Method method = methods[i]就可以了,所以最开始向也没想就认为你是该传参数了。结果你下面又重新得到了一便method。两种不同的取得需要的method的方法你选一个就可以了。 HashMap遍历 java中string类型转换成utf-8,大侠帮我看看 最大,最全的电子书,源码下载基地 诚请各位高手给点学Java的建议~谢谢~~ 怎么让system.out打开一个Dos窗口输出啊? log4j问题 Java巨难问题!如何在程序运行时显示运行进度(不用线程)。不够加分! 今天搞了第一个程序就出错,我快崩溃了,帮帮忙吧! 关于组件(初学者,请大家帮忙) 帮忙,求助!! 请问如何扑获jdbc抛出的关于数据库插入操作时主键重复的错误? 我是新手,问一个笨问题.
。。
}
return testA;
}
public String getTestB(String testB) {
return testB;
}
}
Method method = c.getMethod("get" + getClassName,cls);
搞错了,不好意思,这回应该可以了
import java.lang.reflect.Method;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class Test {
private String testA;
private String testB;
public static void main(String args[])throws IOException{
Test t = new Test();
t.getXXX( "TestA" );
}
public String getTestA(String testA) {
System.out.println( testA );
return testA;
} public String getTestB(String testB) {
return testB;
} public int getXXX(String getClassName) {
int no = 0;
if(getClassName!=null){
Class c = this.getClass();
Method[] methods = c.getMethods();
for(int i=0;i<methods.length;i++){
String className = methods[i].getName();
if(className.length()>3&&className.substring(0,3).equals("get")){
if(className.substring(3).equals(getClassName)){
try {
Class[] cls = {String.class};
Method method = c.getMethod("get" + getClassName,cls);
method.invoke(this, new Object[]{"hehe"}); }catch (Exception ex) {
System.out.println(" get"+getClassName+" 'not found ");
ex.printStackTrace();
}
}
}
}
}
return no;
}
}
Test t = new Test();
t.getXXX( "TestC" );//Test类没有TestC方法并不抛出异常呢?你试试看