是否可以通过类的反射来调用方法?
class MyObject {
... //some methods
public void fce() {
//call another method of this object via reflection?
}
}
谢谢。
最佳答案
绝对:
import java.lang.reflect.*;
public class Test
{
public static void main(String args[]) throws Exception
{
Test test = new Test();
Method method = Test.class.getMethod("sayHello");
method.invoke(test);
}
public void sayHello()
{
System.out.println("Hello!");
}
}
如果您遇到问题,请提出具体问题(最好是用一个简短但完整的程序来演示问题),我们会尽力解决。
关于java - 使用反射调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/566418/