请看下面的代码:
import java.lang.reflect.Method;
class MethodInvokeTest {
public static void main(String[] args) throws Exception {
String str = "hello";
Method m = str.getClass().getMethod("toUpperCase");
System.out.println(m.invoke(str)); //HELLO
}
}
本文通过一个简单的Java程序演示了如何使用反射机制调用字符串对象的toUpperCase方法,并打印转换后的结果。此示例有助于理解Java反射的基本用法。
554

被折叠的 条评论
为什么被折叠?



