【发布时间】:2019-05-07 16:37:59
【问题描述】:
我正在编写一个带有toString() 方法的子类,我希望在数组列表中读取该子类的项目时打印该方法,但是当该类型的项目被读取时,它只会打印超类的toString()被添加到数组列表中。这是子类的代码:
package shop;
import java.text.DecimalFormat;
public class MultiBuyProduct extends Product{
private int minDiscountedQuantity;
private int discountPercent;
public MultiBuyProduct(String name, double price, int quantity, int minDiscountedQuantity, int discountPercent) {
super(name, price, quantity);
this.minDiscountedQuantity = minDiscountedQuantity;
this.discountPercent = discountPercent;
}
//getters and setters
public double getTotalPrice() {
if (getQuantity() >= getMinDiscountedQuantity()) {
double total = getPrice() * getQuantity();
double discountedTotal = total - ((discountPercent/100) * total);
return discountedTotal;
}
return getPrice() * getQuantity();
}
public double discount() {
double total = getPrice() * getQuantity();
double discount = (discountPercent/100) * total;
return discount;
}
@Override
public String toString() {
DecimalFormat format = new DecimalFormat("#.00");
return String.format("%s,\n%20s%5.2f)", super.toString(), format.format(getTotalPrice()), "(Multibuy Discount: GBP ", discount());
}
}
这是超类Products中的toString
public String toString() {
DecimalFormat format = new DecimalFormat("#.00");
return String.format("%3d * GBP %5s %-20s= GBP %7s", quantity, format.format(price),
name, format.format(getTotalPrice()));
}
我这里是包含主要方法的类的一部分,ShoppingCart:
public class ShoppingCart {
private ArrayList<Product> cart;
public ShoppingCart() {
cart = new ArrayList<>();
}
@Override
public String toString() {
double total = 0;
StringBuilder sb = new StringBuilder();
for (Product p : cart) {
sb.append(p.toString()).append("\n");
total += p.getTotalPrice();
}
sb.append(String.format("%48s \n%40s%8.2f", "------------", "TOTAL GBP", total));
return sb.toString();
}
public static void main(String[] args) {
ShoppingCart newCart = new ShoppingCart();
Product apple, milk, caulk, ice, snakes;
MultiBuyProduct snakesMulti;
apple = new Product("Apples (4 pack)", 1.20, 1);
milk = new Product("Milk (1l)", 0.75, 1);
caulk = new Product("Caulk (1l)", 6.84, 1);
ice = new Product("Ice (1kg)", 4.30, 1);
snakes = new Product("Snake (5m)", 32.0, 1);
snakesMulti = new MultiBuyProduct("Snakes", 30.0, 12, 3, 20);
newCart.add(apple);
newCart.add(apple);
newCart.add(apple);
newCart.add(caulk);
newCart.add(milk);
newCart.add(milk);
newCart.add(snakes);
newCart.add(ice);
newCart.add(ice);
newCart.add(snakesMulti);
System.out.println(newCart);
}
哪个打印:
3 * GBP 1.20 Apples (4 pack) = GBP 3.60
1 * GBP 6.84 Caulk (1l) = GBP 6.84
2 * GBP .75 Milk (1l) = GBP 1.50
1 * GBP 32.00 Snake (5m) = GBP 32.00
2 * GBP 4.30 Ice (1kg) = GBP 8.60
12 * GBP 30.00 Snakes = GBP 360.00
------------
TOTAL GBP 412.54
但它应该打印:
3 * GBP 1.20 Apples (4 pack) = GBP 3.60
1 * GBP 6.84 Caulk (1l) = GBP 6.84
2 * GBP .75 Milk (1l) = GBP 1.50
1 * GBP 32.00 Snake (5m) = GBP 32.00
2 * GBP 4.30 Ice (1kg) = GBP 8.60
12 * GBP 30.00 Snakes = GBP 288.00
(Multibuy Discount: GBP 72.00
------------
TOTAL GBP 340.54
我是否需要 MultiBuyProduct 本身的主要方法,或者我可以使用 ShoppingCart 中的主要方法吗?如果需要,我可以为上下文提供更多代码。
编辑:我找到了问题的根源。在ShoppingCart.add() 中,我检查了该项目,如果它不在arraylist 中,它会创建该项目的副本并将其添加到arraylist:
public void add(Product p) {
if (cart.size() > 0) {
for (Product i : cart) {
if (i.getName().equals(p.getName())
&& i.getPrice() == p.getPrice()) {
i.setQuantity(i.getQuantity() + p.getQuantity());
return;
}
}
cart.add(new Product(p)); //this is done because if it were just cart.add(p), it would change the product being assigned as well as the product in the arraylist
} else {
cart.add(new Product(p));
}
}
Product (p) 在Product 中定义为
public Product(Product p) {
this.name = p.name;
this.price = p.price;
this.quantity = p.quantity;
}
这意味着MultiBuyProduct 类型的任何项目都会丢失它们的minDiscountedQuantity 和discountPercent 值。我不确定如何解决这个问题,因为我无法将 public Product(Product p) 扩展到 MultiBuyProduct
【问题讨论】:
-
System.out.println(newCart);调用newCart的toString,这是一个ShoppingCart- 你还没有发布。所以我只能假设它永远不会调用你的toString()的MultiBuyProduct -
我已经更新了我的其他
toString()方法。我相信ShoppingCart它将Product类型的每个项目添加到toString()并且由于MultiBuyProduct是Product的子类,这也应该将multibuyproducts 添加到toString(),对吗?我还展示了 ShoppingCart 是如何构建的。
标签: java overriding subclass tostring