Java 构造函数未定义?
好的,我正在为学校做一项作业,我设置了我的主类和另一个名为 Transaction 的类。在我的主类中,我有:
Transaction t = new Transaction();
并且 Transaction 带有下划线:它表示构造函数未定义。为什么?!
Transaction 类看起来像这样:
public class Transaction {
private String customerNumber, fName, lName, custAddress, custCity;
private int custZip, custPhone;
/** Constructor*/
public Transaction(String a, String b, String c, String d, String e, int f, int g){
this.customerNumber = a;
this.fName = b;
this.lName =c;
this.custAddress = d;
this.custCity = e;
}
看起来它应该可以工作,但事实并非如此。即使当我将一堆变量插入到 main 中创建新 Transaction 对象的位置时,它仍然显示未定义。有人请帮忙!
Ok, I am working on an assignment for school, and I set up my main class and also another class called Transaction. In my main class I have:
Transaction t = new Transaction();
And Transaction is underlined: it says that the constructor undefined. WHY?!
The Transaction class looks like this:
public class Transaction {
private String customerNumber, fName, lName, custAddress, custCity;
private int custZip, custPhone;
/** Constructor*/
public Transaction(String a, String b, String c, String d, String e, int f, int g){
this.customerNumber = a;
this.fName = b;
this.lName =c;
this.custAddress = d;
this.custCity = e;
}
It looks like it should just work, but it's just not. Even when I plug in a bunch of variables into where I make the new Transaction object in main, it still says undefined. Somebody please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的类中没有默认构造函数定义。
当您提供至少一个参数化构造函数的定义时,编译器不再为您提供默认构造函数。
There is no default constructor definition in your class.
When you provide the definition of at least one parameterized constructor the compiler no longer provides you the default constructor.
这是因为您尚未声明不带参数的构造函数。
当您根本没有定义构造函数时,系统会自动为您定义一个没有参数的默认构造函数。
但是现在您已经声明了一个带参数的构造函数,您现在需要传递它们或声明另一个不带参数的构造函数。
This is because you haven't declared a constructor with no arguments.
When you have no constructor defined at all, there is a default constructor with no arguments defined automatically for you.
But now that you've declared a constructor with arguments, you now need to pass them or declare another constructor with no arguments.
您需要创建一个默认构造函数(不带参数的构造函数)。
You need to make a default constructor (one that takes no arguments).
那些说你没有默认构造函数因为你编写了带参数的构造函数的人正在思考 C++。对于 C++ 是这样,但对于 Java 则不然。不存在默认构造函数这样的东西。您必须为您的类编写任何构造函数。如果您不打算构造任何对象,则不必有构造函数。
Those guys that said that you that there is no default constructor because you coded a constructor with arguments are thinking C++. That's true for C++ but not for Java. There is no such thing as a default constructor. You have to code any constructor for your class. You don't have to have a constructor if you're not going to construct any objects.