Java 为什么不';当我在Netbeans中运行以下程序时,我看不到它的输出吗?
该程序编译后没有出现错误。当我在最新的Netbeans(安装了最新的Java)中运行程序时,我看不到输出。 我从《Java 7第三版》第5章中获取了代码的思想。讨论的主题是使用java.lang.class和在不使用新运算符的情况下创建对象Java 为什么不';当我在Netbeans中运行以下程序时,我看不到它的输出吗?,java,object,new-operator,java.lang.class,Java,Object,New Operator,Java.lang.class,该程序编译后没有出现错误。当我在最新的Netbeans(安装了最新的Java)中运行程序时,我看不到输出。 我从《Java 7第三版》第5章中获取了代码的思想。讨论的主题是使用java.lang.class和在不使用新运算符的情况下创建对象 package java7thirdeditionpart1; public class creatObjectWithoutNewOperator { public static void main(String[] args) {
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.
您可能有一个错误并捕获它,但是您没有在控制台上打印它,因此认为程序工作正常 作为最佳实践,切勿在不打印错误的情况下留下捕捉块。否则,程序将捕获错误,但不会显示警告消息
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.
}//类creatObjectWithoutNewOperator到此结束。尝试在方法forName()中的类之前使用包名
使用
e.printStackTrace()重新运行程序在每个catch
块中编码>(分别为e1
和e2
),并亲自查看。原因号2483723:空catch
块总是(是的,总是)一个坏主意。如果您创建了一个可以完全忽略(甚至不记录)的异常,那么您就误用了异常代码>异常,使用e.printStackTrace()代码>获取所有信息。我使用了e1.printStackTrace();和e2.printStackTrace();但该程序在Netbeans IDE中运行并给出消息BUILD SUCCESSFUL(总时间:1秒),但我没有看到输出。@AjmalKhan我认为您只是使用Maven构建项目,但根本没有运行它。我使用右菜单命令“Run File”,因为该文件有自己的主方法,所以输出应该是可见的。我对其他小程序也做了同样的处理,每个小程序都在一个单独的文件中,有自己的主方法,它们工作正常。因此,我不运行项目,但只运行当前文件。
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.