qq_37548112
2017-03-27 11:49
采纳率: 100%
浏览 7.9k
已采纳

Java反射 java.lang.NoSuchMethodException

 public static void testTT() throws Exception {
        Class<?> clazz = Class.forName("reflect.Person");
        // 获得构造器
        Constructor<?> constructor = clazz.getDeclaredConstructor(new Class[] {});
        // 根据类的默认构造器来获得一个对象
        Object instance = constructor.newInstance(new Object[] {});
        System.out.println(instance);

        Constructor<?> constructor2 = clazz
                .getDeclaredConstructor(new Class[] { Integer.class, String.class, Integer.class });
        Object instance2 = constructor2.newInstance(new Object[]{1,"Tom",21});
        System.out.println(instance2);
    }
 public Person(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
 Exception in thread "main" java.lang.NoSuchMethodException: reflect.Person.<init>(java.lang.Integer, java.lang.String, java.lang.Integer)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getDeclaredConstructor(Unknown Source)
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

6条回答 默认 最新

  • 徐枫丶Forever 2017-03-27 13:22
    最佳回答

    Constructor<?> constructor2 = clazz.getDeclaredConstructor(new Class[] { int.class, String.class, int.class });

    评论
    解决 无用
    打赏 举报
查看更多回答(5条)

相关推荐 更多相似问题