猿问

JAVA 反射机制 运行后发生这样的错误提示是什么意思?

public class test1 {
public static void main(String[] args)throws Exception {
Class<?> cls = Class.forName("demo6.Book");
Book book = (Book)cls.newInstance();
book.setTitle("JAVA");
book.setPrice(32);
System.out.println(book);
}
}

Exception in thread "main" java.lang.InstantiationException: demo6.Book

at java.lang.Class.newInstance(Unknown Source)
at demo6.test1.main(test1.java:8)
Caused by: java.lang.NoSuchMethodException: demo6.Book.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
... 2 more


慕标5832272
浏览 660回答 1
1回答

12345678_0001

检查Book类是否有无参构造newInstance只支持无参构造如果Book类有带参构造,没有无参构造就会报异常如果不是这个原因,请把Book类代码贴出来
随时随地看视频慕课网APP

相关分类

Java
我要回答