Java /反射 - 哪里出错?

Alf*_*Alf 3 java reflection

我在这个网站上发现了一些关于java/reflection的帖子.但还是听不懂东西.谁能告诉我代码中的错误?(需要打印"你好!")

输出:

java.lang.NoSuchMethodException: Caller.foo()
Run Code Online (Sandbox Code Playgroud)

这是我的Main.java:

import java.lang.reflect.*;

class Main {

    public static void main(String[] args) {
        Caller cal = new Caller();
        Method met;
        try {
            met = cal.getClass().getMethod("foo", new Class[]{});
            met.invoke(cal);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}

class Caller {
    void foo() {
        System.out.println("HELLO!");
    }
}
Run Code Online (Sandbox Code Playgroud)

Kep*_*pil 8

getMethod()只找到public方法.要么将方法的访问修饰符更改Caller#foo()public,要么使用getDeclaredMethod().