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

Posted programb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何通过反射调用对象的方法?相关的知识,希望对你有一定的参考价值。

请看下面的代码:

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

}

}

以上是关于如何通过反射调用对象的方法?的主要内容,如果未能解决你的问题,请参考以下文章