如何通过反射调用对象的方法?

请看下面的代码:

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

}

}

猜你喜欢

转载自www.cnblogs.com/programb/p/13019683.html