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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© DOOR 中级黑马   /  2014-1-9 23:31  /  1344 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 DOOR 于 2014-1-15 02:05 编辑
  1. import java.lang.reflect.Field;
  2. import java.lang.reflect.Constructor;
  3. class ReflectPoint
  4. {
  5.         public int x;
  6.         public int y;
  7.         private String str1 = "ball";
  8.         private String str2 = "basketball";
  9.         private String str3 = "itcast";
  10.         ReflectPoint(int x,int y)
  11.         {
  12.                 this.x=x;
  13.                 this.y=y;
  14.         }

  15.         public String toString()
  16.         {
  17.                 return str1+":"+str2+":"+str3;
  18.         }
  19. }
  20. class Demo0707
  21. {
  22.         public static void main(String[] args)throws Exception
  23.         {
  24.                 //ReflectPoint rp = new ReflectPoint(3,5);
  25.                 Constructor constructor1 = ReflectPoint.class.getConstructor(int.class,int.class);
  26.                 ReflectPoint rp = (ReflectPoint)constructor1.newInstance(3,5);
  27.                 //changeStringValue(rp);
  28.                 System.out.println(rp);
  29.         }
  30. }
复制代码
NoSuchMethodException异常,求解

评分

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

查看全部评分

8 个回复

倒序浏览
NoSuchMethodException,我汗,你直接英文翻译赛,就是没有找个这个方法异常
回复 使用道具 举报
daoyua 发表于 2014-1-9 23:34
NoSuchMethodException,我汗,你直接英文翻译赛,就是没有找个这个方法异常

不是这个意思,我想问为什么会抛这个异常
回复 使用道具 举报
NoSuchMethodException这个异常指:找不到该方法。
该方法指: constructor1.newInstance(3,5);
改成constructor1.newInstance();即可

newInstance()方法不能带参数,即没有带参数的newInstance()方法

评分

参与人数 1技术分 +1 收起 理由
田磊阳 + 1

查看全部评分

回复 使用道具 举报
我试了一下,可能是你的 ReflectPoint(int x,int y)这个构造函数的权限的问题,你在构造函数前加上public修饰符再试试,应该可以的。
回复 使用道具 举报
请问一下构造方法的默认是啥????还有
newInstance()可以有参数!!!应该是权限的问题!!!
回复 使用道具 举报

ReflectPoint.class.getConstructor(int.class,int.class);
改成
ReflectPoint.class.getDeclaredConstructor(int.class,int.class);
就可以了。
回复 使用道具 举报
编译:
  1. Exception in thread "main" java.lang.NoSuchMethodException: ReflectPoint.<init>(int, int)
复制代码


修改为:
  1. import java.lang.reflect.Field;
  2. import java.lang.reflect.Constructor;
  3. class ReflectPoint
  4. {
  5.         public int x;
  6.         public int y;
  7.         private String str1 = "ball";
  8.         private String str2 = "basketball";
  9.         private String str3 = "itcast";
  10.         
  11.         public ReflectPoint(int x,int y)
  12.         {
  13.                 this.x=x;
  14.                 this.y=y;
  15.         }

  16.         public String toString()
  17.         {
  18.                 return str1+":"+str2+":"+str3;
  19.         }
  20. }
  21. public class InvokeRun
  22. {
  23.         public static void main(String[] args)throws Exception
  24.         {
  25.                 //ReflectPoint rp = new ReflectPoint(3,5);
  26.                 Constructor constructor1 = ReflectPoint.class.getConstructor(int.class,int.class);
  27.                 ReflectPoint rp = (ReflectPoint)constructor1.newInstance(3,5);
  28.                 //changeStringValue(rp);
  29.                 System.out.println(rp);
  30.         }
  31. }
复制代码


构造函数权限不可以被反射获取到,把构造方法公开暴露后,编译通过
输出:ball:basketball:itcast
回复 使用道具 举报
权限问题,下面的都不能访问到上面的reflectPoint方法:
  1. package first;

  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.InvocationTargetException;

  4. class ReflectPoint{
  5.         public int x;
  6.         public int y;
  7.         private String str1="123";
  8.         private String str2="zhangsan";
  9.         private String str3="lisi";
  10.         public ReflectPoint(int x, int y)
  11.         {
  12.                 this.x=x;
  13.                 this.y=y;
  14.         }
  15. //        公有一个转字符串的方法
  16.         public String toString()
  17.         {
  18.                 return str1+":"+str2+":"+str3;               
  19.         }       
  20. }
  21. public class Test{
  22.         public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  23.                 Constructor constructor1=ReflectPoint.class.getConstructor(int.class,int.class);
  24.                 ReflectPoint rp=(ReflectPoint) constructor1.newInstance(3,5);
  25.                 System.out.println(rp);
  26.         }
  27. }
复制代码

改成这样就可以了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马