[英]Why don't I see the output of the following program when I run it in Netbeans?
程序编译并且没有错误。 当我在最新的Netbeans(已安装最新Java)中运行程序时,看不到输出。 我已经从Java 7第三版第5章中获得了代码的想法。正在讨论的主题是使用java.lang.class和创建对象而不使用new运算符。
package java7thirdeditionpart1;
public class creatObjectWithoutNewOperator {
public static void main(String[] args) {
Class myClass2 = null;
try {
myClass2 = Class.forName("Book");
} catch (ClassNotFoundException e) {
}
if (myClass2 != null) {
try {
//Creating an instance of the Book class
Book book1 = (Book) myClass2.newInstance();
book1.setAuthor("Khan");
System.out.println(book1.getAuthor());
book1.setTitle("Second Book");
book1.setIsbn("kh_s_b");
book1.printBookDetails();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (InstantiationException e2) {
e2.printStackTrace();
}
}
}//main method ends here.
}//class creatObjectWithoutNewOperator ends here.
package java7thirdeditionpart1;
public class Book {
String isbn;
String title;
String author;
public Book()
{
this.setIsbn("");
this.setTitle("");
this.setAuthor("");
}//Constructor ends here.
public Book(String isbn, String title, String author) {
this.setIsbn(isbn);
this.setTitle(title);
this.setAuthor(author);
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public void printBookDetails(){
System.out.println("*********************");
System.out.println("ISBN: " + this.getIsbn());
System.out.println("Title: " + this.getTitle());
System.out.println("Author: " + this.getAuthor());
System.out.println("*********************");
}//method printBookDetails ends here.
}//Class Book ends here.
您可能有一个错误并捕获了它,但是没有在控制台上打印它,因此认为程序可以正常运行。
最佳实践是不要在没有某种形式的打印错误的情况下离开catch块。 否则,程序将捕获错误,但不会显示警告消息。
public static void main(String[] args) {
Class myClass2 = null;
try {
myClass2 = Class.forName("Book");
} catch (ClassNotFoundException e) {
System.out.println("Error: " + e);
}
if (myClass2 != null) {
try {
//Creating an instance of the Book class
Book book1 = (Book) myClass2.newInstance();
book1.setAuthor("Khan");
System.out.println(book1.getAuthor());
book1.setTitle("Second Book");
book1.setIsbn("kh_s_b");
book1.printBookDetails();
} catch (IllegalAccessException e1) {
System.out.println("Error1 " + e1);
} catch (InstantiationException e2) {
System.out.println("Error2 " + e2);
}
}
}//main method ends here.
} // class creatObjectWithoutNewOperator在这里结束。
尝试在forName()方法中的类之前使用包名称
package java7thirdeditionpart1;
public class creatObjectWithoutNewOperator {
public static void main(String[] args) {
Class myClass2 = null;
try {
myClass2 = Class.forName("java7thirdeditionpart1.Book");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (myClass2 != null) {
try {
//Creating an instance of the Book class
/*Since newInstance returns a
java.lang.Object object, you need to downcast it to its
original type.*/
Book book1 = (Book) myClass2.newInstance();
book1.setAuthor("Khan");
book1.setTitle("Second Book");
book1.setIsbn("kh_s_b");
book1.printBookDetails();
book1 = (Book) myClass2.newInstance();
book1.setAuthor("Ajmal");
book1.setTitle("First Book");
book1.setIsbn("aj_f_b");
book1.printBookDetails();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (InstantiationException e2) {
e2.printStackTrace();
}
}
}//main method ends here.
}//class creatObjectWithoutNewOperator ends here.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.