开发者社区 问答 正文

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

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

展开
收起
游客pklijor6gytpx 2019-12-02 16:13:31 738 分享
分享
版权
举报
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 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
问答地址:
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等