java 反射 get方法_java反射调用方法

java 反射 get方法_java反射调用方法I’mworkingwiththebasicsofJavareflectionandobservinginformationonmethodsofclasses.IneedtogetamethodthatmatchesspecificationsasdescribedbythegetMethod()function.However,…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

java 反射 get方法_java反射调用方法

I’m working with the basics of Java reflection and observing information on methods of classes. I need to get a method that matches specifications as described by the getMethod() function. However, when I do this I get a NoSuchMethodException, and I was hoping you could tell me why my implementation is incorrect.

static void methodInfo2(String className) throws ClassNotFoundException,

NoSuchMethodException{

Class cls = null;

try{

cls = Class.forName(className);

} catch(ClassNotFoundException e){

e.printStackTrace();

}

System.out.println(“Cls: “+cls);

Method method1 = cls.getMethod(“test”, null);

System.out.println(“method1: “+method1);

}

EDIT1:When I print out “Cls: “+cls, the output is “Cls: class a8.myclass2”. Why does it append the class part? (the a8 is correct, so don’t worry about that) /EDIT1

This is the function I use to read in a class from my main function, and then I want to getMethod() with the parameters “test” and null, where “test” is the name of the method and null means the method has no parameters. The class I am reading in is called myclass2 which is here:

package a8;

public class myclass2 {

void test(){

//”takes no parameters”

//”returns bool”

//”name starts with test”

//return true;

}

}

As you can see, the method does infact exist in the class. If you could point out my mistake, I would really appreciate it.

解决方案

Make your test method public. I believe Class.getMethod() is limited to public methods.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/190346.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • js实现图片上传到服务器和回显

    js实现图片上传到服务器和回显

    2021年11月10日
    244
  • BCG界面库

    BCG界面库之前用过BCG界面库中的表格控件,深感其强大,现在再来用一下其它的。一.关于BCGControlBar。BCGControlBar是一个基于MFC的扩展库,您可以通过完全的用户化操作构成一些类似于M

    2022年7月3日
    31
  • 复制粘贴不到远程桌面_win10远程桌面无法复制粘贴

    复制粘贴不到远程桌面_win10远程桌面无法复制粘贴在远程服务器上打开任务管理器,在进程里面找到rdpclip进程(或者剪贴板监视器),点击“结束进程”打卡DOS命令符,输入rdpclip后,确认,从新打开远程剪贴服务。

    2022年8月20日
    10
  • jsp+ajax_javascriptjavascript日

    jsp+ajax_javascriptjavascript日明后两天梁言兵老师来讲Ajax及其最近作过的一个真实的Ajax项目,所以,我今天讲解梁老师的课程所需要的一些前置知识。因为大家对Javascript不是很熟悉,所以我首先讲解Javascript的DHTML功能。本来入学考试要求大家很好地掌握Javascript的,但是大家都不能理解我们的苦衷,并没有专心去对待Javascript。想想我前两年强调javascript和css的重要性时,一些培训中

    2025年10月27日
    1
  • 简述信道带宽的概念_传输信道带宽是什么意思

    简述信道带宽的概念_传输信道带宽是什么意思在写这篇文章之前我一直对“带宽”这个用语比较迷惑,比如我们通常说“带宽10M的网络”,“网络带宽是10M”等等,很容易跟信道的带宽比如10MHZ、100MHZ相混淆。我查阅了相关的资料,终于在通信和电路上将这两个概念彻底搞清楚了。1、网络带宽我们通常口头上说的网络带宽,用标准的术语来说应该是信道容量,单位为比特每秒(bit/s)。在通信原理中用香农公式计算出…

    2022年10月11日
    2
  • chrome插件演示,经js转让chrome api清除浏览器缓存

    chrome插件演示,经js转让chrome api清除浏览器缓存

    2022年1月12日
    58

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号