0

我正在用java编写一个程序,我必须创建一个包含main的类(InvoiceTest)和第二个类(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");
    }
}
4

3 回答 3

0

它们都在同一个.java文件中吗?java 编译器要求每个源文件只有一个 java 类。

于 2013-07-03T01:07:53.440 回答
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();
    }
}
于 2013-07-03T01:12:36.137 回答
0

确实是奇怪的错误。

添加回类的构造函数怎么样Invoice

公共发票(){}

于 2013-07-03T01:16:32.097 回答