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.

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • 再次研究消息队列记的笔记——activemq

    再次研究消息队列记的笔记——activemq

  • Java File中renameTo的介绍和使用说明

    看到项目中有个地方用的File的renameTo这个方法,本以为这个方法就是用来文件重命名的,但是项目中确用它来进行文件的移动。就是使用renameTo将一个文件从一个目录移动另一个目录下!下面就简单介绍一下renameTo,并整理一个renameTo的使用!renameTo的介绍官方文档说明/** * 重新命名此抽象路径名表示的文件。 此方法行为的许多方面都是与平台有关的:重命名操作无法

  • 使用 Captcha 扩展包 为 Laravel 5 应用生成验证码

    使用 Captcha 扩展包 为 Laravel 5 应用生成验证码

    2021年10月26日
  • Unity 协程嵌套测试

    Unity 协程嵌套测试通过return嵌套,子协程执行完之后父进程才继续执行usingSystem.Collections;usingUnityEngine;publicclasstest2:MonoBehaviour{ voidStart(){Debug.Log(“Start0”);StartCoroutine(test());De

  • Linux文件系统详解

    从操作系统的角度详解Linux文件系统层次、文件系统分类、文件系统的存储结构、不同存储介质的区别(RAM、ROM、Flash)、存储节点inode。本文参考:http://blog.chinaunix.net/uid-8698570-id-1763151.htmlhttp://www.iteye.com/topic/816268http://soft.chinabyte.com/os/142…

  • Linux压缩打包命令——tar、zip、unzip

    打包跟压缩的区别:打包是指将多个文件或者目录放在一起,形成一个总的包,这样便于保存和传输,但是大小是没有变化的,压缩是指将一个或者多个大文件或者目录通过压缩算法使文件的体积变小以达到压缩的目的,可以节省存储空间,在压缩的时候通常是先打包再压缩;tar命令tar命令参数前面加”-“与不加“-”的区别:tar命令参数前面加不加“-”执行命令的结果是没有区别的,区别只要是在与linux风…

发表回复

您的电子邮箱地址不会被公开。

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