简体   繁体   English

具有相同名称但签名不同的构造函数无法运行

[英]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.

相关问题 当方法具有相同名称但签名不同时,方法是否被覆盖? - Are methods overridden when methods have the same name but different signatures? 具有不同签名但功能相同的功能 - Functions with different signatures, but the same body Java编译器:具有相同名称和不同签名的两个方法如何匹配方法调用? - Java compiler: How can two methods with the same name and different signatures match a method call? 使用相同的方法名称但使用import static具有不同签名的情况下出现意外的编译错误 - Unexpected compile error when using the same method name but with different signatures using import static 如果签名不同,则Java编译器禁止在内部类方法中使用与外部类相同的名称进行创建 - Java compiler prohibit the creation in the inner class method with same name as in the outer class if the signatures are different 相同的Java代码无法在不同的计算机上运行 - Same java code does not run on different machines HmacSHA1使用相同的密码在不同的系统上生成不同的签名 - HmacSHA1 generates different signatures on different systems using same secret 名称与类名不同的构造方法 - Constructor with name different to the class name Java名称冲突错误,尽管方法签名不同 - Java name clash error, despite different method signatures 与构造函数同名的方法 - 为什么? - Methods With Same Name as Constructor - Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM