想要在运行时执行一个函数,函数名是从运行时获得的:
public static void callClassname() throws Exception
{
String[] getExecute =getInputdata.getEligibleColoumn();
for(int i=0 ; i < getExecute.length ; i++)
{
getExecute[i] = getExecute[i].concat("()");String test = "classname." .concat(getExecute[i]);
System.out.println("Test Passed:" + test);
}
}
在上面的代码测试中,将给出结果classname.functionname()
。我想执行它或调用它。你能帮我怎么做,还是其他更好的方法? PLZ建议。
答案 0 :(得分:0)
解决此问题和调用实例方法的最佳方法是使用Java Reflection API
答案 1 :(得分:0)
您正在寻找的是在运行时将String转换为代码,您可以使用BeanShell来实现此目的。
你可以从这里下载:
http://www.beanshell.org/download.html
您还可以查看Java Compiler API:
https://www.javacodegeeks.com/2015/09/java-compiler-api.html