程序编译并且没有错误。当我在最新的Netbeans(最新Java安装)中运行程序时,我看不到输出。 我已经从Java 7第3版第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.
答案 0 :(得分:0)
您可能遇到错误并抓住了它,但是您没有在控制台上打印它,因此认为程序运行正常。
作为最佳做法从不在没有某种形式的打印错误的情况下留下一个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在这里结束。
答案 1 :(得分:0)
尝试在方法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.