java 反射constructor_java 反射 constructors的问题,真心需要解决

java.lang.IllegalArgumentException:wrongnumberofargumentsatsun.reflect.NativeConstructorAccessorImpl.newInstance0(NativeMethod)atsun.reflect.NativeConstructorAccessorImpl...

java.lang.IllegalArgumentException: wrong number of arguments

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)

at java.lang.reflect.Constructor.newInstance(Constructor.java:513)

at test._01reflect.Reflect.main(Reflect.java:17)

源代码是:

public class Student {

private int age;

private String name;

public Student() {

super();

}

public Student(String name) {

super();

this.name = name;

}

public Student(int age) {

super();

this.age = age;

}

public Student(int age, String name) {

super();

this.age = age;

this.name = name;

}

}

main方法:

public static void main(String[] args) {

try {

Class cls = Class.forName("test._01reflect.Student");

Student stu = null;

//获取构造函数

Constructor>[] con = cls.getConstructors();

stu = (Student)con[0].newInstance();//第17行

stu = (Student)con[1].newInstance(25);

stu = (Student)con[2].newInstance("maosheng");

stu = (Student)con[3].newInstance(25,"maosheng");

各位大神,这是哪里的错误啊,小弟实在解决不了

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值