A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张绍成 黑马帝   /  2011-12-28 20:23  /  1542 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张绍成 于 2011-12-29 19:25 编辑
  1.     Class cls = Class.forName("chb.test.reflect.Student");   
  2.       Method setMethod = cls.getDeclaredMethod("setAge",Integer.class);   
  3.       setMethod.invoke(cls.newInstance(), 15);  
复制代码
错误提示:
  1. Exception in thread "main" java.lang.NoSuchMethodException: cn.itcast.interview.test2.Person.<init>(java.lang.String, java.lang.Integer)
  2.         at java.lang.Class.getConstructor0(Class.java:2721)
  3.         at java.lang.Class.getConstructor(Class.java:1674)
  4.         at cn.itcast.interview.test2.IntrospectorTest.main(IntrospectorTest.java:22)
复制代码

3 个回复

倒序浏览
本帖最后由 吴上储 于 2011-12-28 20:54 编辑

  Class cls = Class.forName("chb.test.reflect.Student");   

02.      Method setMethod = cls.getDeclaredMethod("setAge,"int.class);   

03.      setMethod.invoke(cls.newInstance(), 15);
改成int.class试试
java.lang.NoSuchMethodException: cn.itcast.interview.test2.Person.<init>(java.lang.String, java.lang.Integer)

你的age属性是int还是integer?

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

回复 使用道具 举报
cls.newInstance(), 这个相当于是用空参数的构造方法创建一个实例对象,Student里面是不是没有空参数的构造方法
回复 使用道具 举报
Method setMethod = cls.getDeclaredMethod("setAge",Integer.class);   参数中Integer.class应该改为 int.class.它们两个是不一样的。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马