我正在用java编写程序我必须创建一个类(InvoiceTest),它将包含main和第二个类(Invoice)。当我运行主InvoiceTest类时,它应该在Invoice类中显示消息。我一直收到错误
Exception in thread "main" java.lang.ExceptionInInitializerError
at InvoiceTest.main(InvoiceTest.java:4)
Caused by: java.lang.RuntimeException: Uncompilable source code - constructor Invoice() is already defined in class Invoice
at Invoice.<clinit>(Invoice.java:20)
... 1 more
我不知道该怎么做,请让我知道我做错了什么以及如何解决它的任何建议。感谢
public class InvoiceTest{
public static void main(String [] args) {
Invoice invoiceObject = new Invoice();
invoiceObject.simpleMessage();
}
}
// This is the second class. I am using NetBeans so each class is in its own window.
public class Invoice {
public void simpleMessage() {
System.out.println("This is another class");
}
}
答案 0 :(得分:0)
它们是否在同一个.java
文件中? java编译器每个源文件只需要一个java类。
答案 1 :(得分:0)
您不需要两个单独的课程。一节课有效。
public class Invoice {
public void simpleMessage() {
System.out.println("This is another class");
}
public static void main(String[] args) {
Invoice invoiceObject = new Invoice();
invoiceObject.simpleMessage();
}
}
答案 2 :(得分:0)
确实存在奇怪的错误。
如何为类Invoice
添加构造函数?
public Invoice(){ }