![](/img/trans.png)
[英]Why Android Studio prompt “Method doesn't override its superclass”?
[英]Why doesn't my toString() method properly override the toString() method in its superclass?
我正在用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
類型的項目MultiBuyProduct
丟失其minDiscountedQuantity
和discountPercent
值。 我不確定如何解決此問題,因為我無法將public Product(Product p)
擴展到MultiBuyProduct
這是一些示例代碼。 請查看這是否是您的應用程序試圖執行的操作(這是行為)嗎?
該示例具有Animal
類。 Dog
類擴展了 Animal
。 兩者都覆蓋了java.lang.Object
的toString
方法。
class Animal {
private String name;
public Animal(String s) {
name = s;
}
public String toString() {
return "Animal name: " + name;
}
}
class Dog extends Animal {
private String name;
public Dog(String s) {
super(s);
name = s;
}
public String toString() {
return "Dog Name: " + name + ", " + super.toString();
}
}
Public class TestingInheritance {
public static void main(String [] args) {
Animal d0 = new Animal("just animal");
Animal d1 = new Dog("mutt");
Dog d2 = new Dog("pup");
System.out.println(d0);
System.out.println(d1);
System.out.println(d2);
System.out.println("");
List<Animal> anims = Arrays.asList(d0, d1, d2);
for (Animal a : anims) {
System.out.println(a.toString());
}
}
}
輸出:
Animal name: just animal
Dog Name: mutt, Animal name: mutt
Dog Name: pup, Animal name: pup
Animal name: just animal
Dog Name: mutt, Animal name: mutt
Dog Name: pup, Animal name: pup
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.