使用Java反射API调用静态方法
1. 概述
在本快速教程中,我们将讨论如何使用反射API在Java中调用静态方法。
我们将涵盖两种不同的情况:
- 静态方法是公共的。
- 静态方法是私有的。
2. 示例类
为了使演示和解释更简单,我们首先创建一个GreetingAndBye类作为示例:
public class GreetingAndBye {
public static String greeting(String name) {
return String.format("Hey %s, nice to meet you!", name);
}
private static String goodBye(String name) {
return String.format("Bye %s, see you next time.", name);
}
}
大约 2 分钟