[英]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.