【问题标题】:Constructor with same name, but different signatures does not run具有相同名称但不同签名的构造函数不会运行
【发布时间】:2014-10-28 12:52:35
【问题描述】:

我有这 2 个构造函数:

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;
}

在另一个班级这样做:

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);

输出是:

id, name, desc, price

为什么不把数量带入item2??? 如果我这样做:

System.out.println(Integer.parseInt(quantityField.getText()));

它确实给了我数量。

谁能告诉我为什么它没有意识到使用第二个 StockItem 构造函数。即使在删除第一个 StockItem 构造函数后也尝试过。

【问题讨论】:

  • 你甚至不调用构造函数,你调用了一个setter。你的二传手坏了吗?

标签: java methods constructor


【解决方案1】:

对于一个你没有使用你在问题中显示的任何一个构造函数。您正在创建一个新对象,然后使用 setter 设置字段。你可能想看看你的类的 setQuantity 方法,看看它在做什么。此处不使用任何构造函数。

尝试这样的方法来初始化你的对象:

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()));

它实际上会使用你的构造函数。

还要查看 StockItem 类的 toString() 方法。它可能不是打印数量。您需要在 toString() 方法输出中添加数量字段。

【讨论】:

  • 我确实使用过它,它给了我相同的输出。这就是为什么不知道这两个有区别的原因。
  • 是的,有区别。 setter 设置单个字段,而构造函数用于初始化整个对象。无论哪种方式都可以使用默认构造函数然后设置器或使用自定义构造函数,但更好的方法是使用您的自定义构造函数。这就是他们的目的。
【解决方案2】:

StockItemtoString() 方法中,您还必须包含quantity。例如:

public String toString() {
    return id + ", " + name + ", " + description + ", " + price + ", " + quantity;
}

这样,当您执行System.out.println(item2); 时,toString() 将被调用,结果中包含quantity

【讨论】:

  • 它确实有效,但是如果在其他地方使用了 4 个签名构造函数,我可以用 4 个签名制作另一个没有数量的 toString 吗?
  • 如果你有相同的toString()签名,没有。
  • @kocko 如果您需要这样做,只需将数量初始化为 "" 空白字符串,如果他们使用其他构造函数,它将不会打印任何内容。
  • @kocko 或者在您的构造函数中使用 4 个输入将数量初始化为 null 并检查您的 toString() 方法是否为 null 如果不是 null 则以另一种方式打印。
  • @kocko 你甚至没有使用你的构造函数。我展示了一些关于如何使用构造函数的代码。
【解决方案3】:

您使用的构造函数不在您在此处显示的构造函数中。你确定你能够在不创建没有参数的新构造函数的情况下运行它吗?如果我没记错的话,你应该像这样使用它们。如果你想使用 first 构造函数:

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);

如果你想使用 second 构造函数:

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);

这称为重载。 :)

P.S:使用变量使其更清晰。 :)

【讨论】:

    猜你喜欢
    • 2011-01-24
    • 2010-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    相关资源
    最近更新 更多