我有那些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。你的二传手是否坏了? – 2014-10-28 12:55:56