开发者社区 问答 正文

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

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

展开
收起
游客pklijor6gytpx 2019-12-02 16:13:31 753 分享 版权
1 条回答
写回答
取消 提交回答
  • 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
        }
    }
    
    2019-12-02 16:13:42
    赞同 展开评论
问答地址: