我正在用java编写一个程序,如果我必须创建一个包含main和第二个类(Invoice)的类(InvoiceTest)。当我运行主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");
}
}
您不需要两个单独的类。一节课有效。
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();
}
}
它们是否都在同一个.java
文件中?java编译器要求每个源文件只有一个java类。
确实是一个奇怪的错误。
如何添加回类Invoice
的构造函数?
公开发票(){}