首页 > 编程语言 > 详细

运行反射时报错java.lang.NoSuchMethodException: com.wang.reflect.Student4.<init>()

时间:2019-11-25 16:58:29      阅读:121      评论:0      收藏:0      [点我收藏+]
public class fieldDemo01 {
public static void main(String[] args) throws Exception {
//通过反射获取成员变量并使用
//1、先获取Student类的字节码文件
Class clazz=Student4.class;
//2、通过字节码对象获取构造器对象,然后创建学生类对象
Constructor con=clazz.getConstructor();
Student4 stu=(Student4)con.newInstance();
Student4 stu1=(Student4)clazz.getConstructor().newInstance();//第二种写法 链式编程
//3、设置学生对象的各个属性值
System.out.println(stu);
}
}
class Student4{
public String name;
private int age;

@Override
public String toString() {
return "Student4{" +
"name=‘" + name + \‘‘ +
", age=" + age +
‘}‘;
}
}


Exception in thread "main" java.lang.NoSuchMethodException: com.wang.reflect.Student4.<init>() 这个错误可以通过在Student4类中添加无参构造方法解决

运行反射时报错java.lang.NoSuchMethodException: com.wang.reflect.Student4.<init>()

原文:https://www.cnblogs.com/wyj96/p/11928406.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!