使用反射调用方法

mic*_*hal 4 java reflection

是否可以通过类的反射来调用方法?

class MyObject {
    ...   //some methods

    public void fce() {
        //call another method of this object via reflection?
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

Jon*_*eet 7

绝对:

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!");
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您遇到问题,请发布一个特定的问题(最好用简短但完整的程序来证明问题),我们会尝试对其进行整理.