[英]Constructor with same name, but different signatures does not run
I have those 2 constructors: 我有那两个构造函数:
public StockItem(Long id, String name, String desc, double price) {
this.id = id;
this.name = name;
this.description = desc;
this.price = price;
}
public StockItem(Long id, String name, String desc, double price, int quantity) {
this.id = id;
this.name = name;
this.description = desc;
this.price = price;
this.quantity = quantity;
}
Doing this in another class: 在另一堂课中这样做:
StockItem item2 = new StockItem();
item2.setId(Long.parseLong(idField.getText()));
item2.setName(nameField.getText());
item2.setDescription(descField.getText());
item2.setPrice((double) Math.round(Double.parseDouble(priceField.getText()) * 10) / 10);
item2.setQuantity(Integer.parseInt(quantityField.getText()));
System.out.println(item2);
The output is: 输出为:
id, name, desc, price
Why does it not take the quantity into the item2??? 为什么不将数量带入项目2 ??? If I do: 如果我做:
System.out.println(Integer.parseInt(quantityField.getText()));
It DOES give me the quantity. 它确实给了我数量。
Could anyone tell me why it does not realize to use the second StockItem constructor. 谁能告诉我为什么没有意识到要使用第二个StockItem构造函数。 Tried it even after deleteing the first StockItem constructor. 即使删除了第一个StockItem构造函数,也进行了尝试。
For one you are not using either constructor you displayed in your question. 对于一个,您没有使用问题中显示的任何一个构造函数。 You are creating a new object then setting the fields using setters. 您正在创建一个新对象,然后使用设置器设置字段。 You may want to take a look at your setQuantity method of your class and see what it is doing. 您可能想看一下类的setQuantity方法,看看它在做什么。 You don't use either constructor here. 您在这里不使用任何一个构造函数。
Try something like this to initialize your object: 尝试这样的事情来初始化您的对象:
StockItem item2 = new StockItem(Long.parseLong(idField.getText()), nameField.getText(), descField.getText(), (double) Math.round(Double.parseDouble(priceField.getText()) * 10) / 10, Integer.parseInt(quantityField.getText()));
It will actually use your constructor. 它实际上将使用您的构造函数。
Also look at your toString() method of your StockItem class. 还要看看StockItem类的toString()方法。 It is probably not printing quantity. 它可能不是打印数量。 You need to add quantity field to your toString() method output. 您需要在toString()方法输出中添加数量字段。
In your toString()
method in the StockItem
you have to include the quantity
as well. 在StockItem
toString()
方法中,还必须包括quantity
。 For example: 例如:
public String toString() {
return id + ", " + name + ", " + description + ", " + price + ", " + quantity;
}
This way when you do System.out.println(item2);
这样当您执行System.out.println(item2);
, the toString()
will be invoked with the quantity
included in the result. ,将使用结果中包含的quantity
调用toString()
。
The constructor you're using it isn't in the ones you're showing here. 您正在使用的构造函数不在此处显示的构造函数中。 Are you sure you're able to run that without making a new constructor with no Arguments? 您确定无需创建没有参数的新构造函数就能运行该函数吗? If I'm not wrong, you should use them like this. 如果我没看错,您应该像这样使用它们。 If you want to use the first constructor: 如果要使用第一个构造函数:
Long id = Long.parseLong(idField.getText());
String name = nameField.getText();
String desc = descField.getText();
double price = (double) Math.round(Double.parseDouble(priceField.getText());
StockItem stockItemUsingFirstConstructor = new StockItem(id, name, desc, price);
And if you want to use the second constructor: 如果要使用第二个构造函数:
Long id = Long.parseLong(idField.getText());
String name = nameField.getText();
String desc = descField.getText();
double price = (double) Math.round(Double.parseDouble(priceField.getText());
int quantity = Integer.parseInt(quantityField.getText());
StockItem stockItemUsingSecondConstructor = new StockItem(id, name, desc, price, quantity);
This is called overloading . 这称为超载 。 :) :)
PS: Used variables to make it more clear. PS:使用变量使其更清晰。 :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.