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


相关推荐

  • smtp服务器组件,本机搭建虚拟SMTP服务器教程[通俗易懂]

    smtp服务器组件,本机搭建虚拟SMTP服务器教程[通俗易懂]该楼层疑似违规已被系统折叠隐藏此楼查看此楼Windows2000用户安装设置服务端WindowsXP和2000本身就拥有构件SMTP服务器的功能,只是一般还没有安装。选择“控制面板→添加/删除程序→添加/删除Windows组件”,弹出“Windows组件向导”对话框,在其中双击“Internet信息服务(IIS)”项,就会打开详细选择项,选中“SMTPService”,按“确定”,插入…

    2022年10月3日
    1
  • 【2021最新版】MyBatis面试题总结(38道题含答案解析)[通俗易懂]

    【2021最新版】MyBatis面试题总结(38道题含答案解析)[通俗易懂]文章目录1、什么是Mybatis?2、Mybaits的优点?3、MyBatis框架的缺点?4、MyBatis框架适用场合5、MyBatis与Hibernate有哪些不同?6、#{}和${}的区别是什么?7、当实体类中的属性名和表中的字段名不一样,怎么办?8、模糊查询like语句该怎么写?9、通常一个Xml映射文件,都会写一个Dao接口与之对应,请问,这个Dao接口的工作原理是什么?Dao接口里的方法,参数不同时,方法能重载吗?10、Mybatis是如何进行分页的?分页插件的原理是什么?11、Myba

    2022年5月1日
    41
  • Pytest(1)安装与入门[通俗易懂]

    Pytest(1)安装与入门[通俗易懂]pytest介绍pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高。根据pytest的官方网站介绍,它

    2022年7月29日
    3
  • Hibernate中 hql多条件查询 mysql hql删除记录

    Hibernate中 hql多条件查询 mysql hql删除记录whereSql+=”andorderStatus=?”;hql删除记录//使用HQL语句删除数据publicvoiddelete(intid){Transactiontran=this.session.beginTransaction();Stringhql=”DeleteFROMMemberWhereid=?”;Queryq=this.session.createQ

    2022年6月29日
    22
  • idea2021.9 激活码【2021最新】[通俗易懂]

    (idea2021.9 激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~S32P…

    2022年3月26日
    56
  • idea配置javaweb开发环境_你没有创建该项目的权限

    idea配置javaweb开发环境_你没有创建该项目的权限IDEA分为社区版和企业版,区别在于,社区版免费,企业版收费,但有30天免费使用期,到期后会提示你进行激活,花钱买实在太贵,可以去淘宝买激活码或使用工具。IDEA官方下载地址https//www.jetbrains.com/idea/download/#section=windows。本文教给各位使用IDEA创建web项目,配置tomcat进行访问,好了,下面进入正题。以本地tomcat方式启动。以DeBug方式启动项目。…

    2022年9月20日
    1

发表回复

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

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