【发布时间】: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