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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • qt没有被正确安装_qt软件安装步骤

    qt没有被正确安装_qt软件安装步骤对于太长不看的朋友,可参考Qt的安装和使用中的常见问题(简略版)。目录1、概述2、Qt简介3、Qt版本3.1查看安装的Qt版本3.2查看当前项目使用的Qt版本3.3查看当前项目使用

    2022年8月1日
    5
  • netCMS学习笔记

    netCMS学习笔记二者关系:“netCMS”出自“风讯”,“netCMS”完全开源,有相关文档资料。“风讯”开源版删除了一些源码。学习内容:一、模板实现原理   标签   netCMS自定义了一系列标签,在模版页面中插入这些标签,在发布网页时类“NetCMS.Publish.Template”用正则表达示筛选出这些标签,然后从数据库表“NT_News”中读取相应的内容,将标签替换。   模版   模版引用的CSS,图片等路径,必需是绝对路径。   前台浏览方式   分“动态”和“静态”两种模式。   在“动态”

    2022年9月30日
    1
  • java swing开发窗体程序开发(一)GUI编程[通俗易懂]

    java swing开发窗体程序开发(一)GUI编程[通俗易懂]JavaSWing就是java提供的一个类库专门用于开发窗体程序的,相对于C#的winform,javaswing的开发要比C#更加麻烦,他不像C#拥有可视化的界面编辑工具而是需要像QT一样纯代码式GUI编程。java的好处就是通用性强,跨平台,这个java窗体程序做好之后,哪都可以用。然而C#的winform只能运行在有.net框架上的平台,然而.net支持最好的,也就只有windows…

    2022年8月23日
    3
  • webpack devtools_webpack loader和plugin的区别

    webpack devtools_webpack loader和plugin的区别首先我们需要知道sourcemap是什么?顾名思义资源映射,它做的就是维护打包处理后的代码与源代码之间的映射关系,只有映射的精确性则取决于webpack的配置项devtool,其决定了项目打包时是否以及如何生成sourcemap,而生成的sourcemap不同决定了构建产物的体积和构建以及重新构建的速度的不同。具体配置项可选值可参考webpack文档这里不一一列举。首先可以看一下webpack的源码,对应处理逻辑仅有20行:https://github.com/webpack/webpack/bl

    2022年10月6日
    3
  • tcpdf的方法_tcp fin

    tcpdf的方法_tcp fin$pdf=newTCPDF(PDF_PAGE_ORIENTATION,PDF_UNIT,PDF_PAGE_FORMAT,true,’UTF-8′,false);页面记得也设为utf-8AddPage();新建一个pdf文档页面。Image($file,$x,$y,$w,$h,$type,$link,$align,$resize,$dpi,$p

    2025年9月1日
    4
  • pycharm怎么返回上一个操作_pycharm界面

    pycharm怎么返回上一个操作_pycharm界面如图勾选tollbar就有“←”图标,点击就可以查看上次代码的位置了

    2022年8月28日
    4

发表回复

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

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