Java - 重写父类

标签 java class overriding parent-child

我是 Java 新手,我正在开发一个项目,用于计算有/没有员工折扣的价格。阅读以下代码后,有人可以向我解释我需要如何更改代码才能获得正确的输出吗?我将在帖子末尾更详细地解释这个问题。

父类(我不允许编辑此内容):

public class GroceryBill {
    private Employee clerk;
    private List<Item> receipt;
    private double total;
    private double internalDiscount;

    public GroceryBill(Employee clerk) {
        this.clerk = clerk;
        receipt = new ArrayList<Item>();
        total = 0.0;
        internalDiscount = 0.0;
    }

    public void add(Item i) {
        receipt.add(i);
        total += i.getPrice();
        internalDiscount += i.getDiscount();
    }

    public double getTotal() {
        return Math.rint(total * 100) / 100.0;
    }

    public Employee getClerk() {
        return clerk;
    }

    public void printReceipt() {
        System.out.println(this);
    }

    private String valueToString(double value) {
        value = Math.rint(value * 100) / 100.0;
        String result = "" + Math.abs(value);
        if(result.indexOf(".") == result.length() - 2) {
            result += "0";
        }
        result = "$" + result;
        return result;
    }

    public String receiptToString() {
        String build = "items:\n";
        for(int i = 0; i < receipt.size(); i++) {
            build += "   " + receipt.get(i);
            if(i != receipt.size() - 1) {
                build += "\n";
            }
        }
        return build;
    }

    public String toString() {
        return receiptToString() + "\ntotal: " + valueToString(total);
    }

    public String discountToString() {
        return receiptToString() + "\nsub-total: " + valueToString(total) + "\ndiscount: " + valueToString(internalDiscount) + "\ntotal: " + valueToString(total - internalDiscount);
    }

    public static class Employee {
        private String name;

        public Employee(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

    public static class Item {
        private String name;
        private double price;
        private double discount;

        public Item(String name, double price, double discount) {
            this.name = name;
            this.price = price;
            this.discount = discount;
        }

        public double getPrice() {
            return price;
        }

        public double getDiscount() {
            return discount;
        }

        private String valueToString(double value) {
            String result = "" + Math.abs(value);
            if(result.indexOf(".") == result.length() - 2) {
                result += "0";
            }
            result = "$" + result;
            return result;
        }

        public String toString() {
            return name + " " + valueToString(price) + " (-" + valueToString(discount) + ")";
        }
    }
}

这是我的代码:

public class DiscountBill extends GroceryBill
{
    private int myDiscountCount;
    private double myDiscountAmount;
    private double myPrice;
    
    public DiscountBill(Employee clerk, boolean preferred)
    {
        super(clerk);
        
        String name = "";
        double price = 0;
        double discount = 0;
        
        GroceryBill.Item myBill = new GroceryBill.Item(name, price, discount);
        myDiscountAmount = myBill.getDiscount();
        
        if (myDiscountAmount > 0 && preferred)
        {
            myDiscountCount++;
        }
    }

    public void add(Item myBill)
    {
        myPrice += myBill.getPrice();
        myDiscountAmount = myBill.getDiscount();

        if (myDiscountAmount > 0 )
        {
            myDiscountCount++;
        }
    }
    public double getTotal()
    {
        if (myDiscountCount > 0)
        {
            return myPrice - myDiscountAmount;
        }
        return myPrice;
    }
    public int getDiscountCount()
    {
        return myDiscountCount;
    }
    public double getDiscountAmount()
    {
        return myDiscountAmount;
    }
    public double getDiscountPercent()
    {
        return ((myPrice - myDiscountAmount) / myPrice * 100);
    }
}

最后,here是预期输出,后跟我的具体问题:

我的方法得到的输出比应有的结果提前了一步。也就是说,例如,我的 getTotal 应该从 1.35 开始(我正在使用的网站输入的第一个值,用于测试我编写的子类),然后经过另一个步骤,它应该减少到 1.1(网站使用构造函数中首选 boolean 值使用员工折扣),但我的程序输出 1.1,因为我的子类覆盖了父类的 getTotal() 方法,并且永远不会以应有的总数 (1.35) 开始。基本上我需要知道如何从父类获取这些原始值,然后使用重写方法来获取更改后的值。如果您想了解本网站如何运作,here是我正在研究的问题的链接。

附注如果我需要提供更多/更少的信息以及清理这篇文章或使其更容易理解的方法,请告诉我。如果我的问题太宽泛,请问我你不明白的地方,我会尽力告诉你!谢谢!

最佳答案

我不确定这是否是你想要的。你想调用父方法吗? 为此,您可以使用 super.parentMethodName 如有错误请指正

关于Java - 重写父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31629464/

相关文章:

java - Java(或任何语言)概率中的随机洗牌

java - Java 8 接口(interface)中静态方法的用途是什么?

c++ - 虚函数+带基类指针的STL容器

java - toString() 不适用于父类(super class)和子类

java - 使用 maven 从配置文件默认系统属性

java - 在 Android 应用程序中设置最小堆大小的目的是什么?

java - 从 JSON 对象获取数组

java - NullPointerException,研究了也找不到原因

Java-私有(private)内部类公共(public)成员

java - 为什么不能在编译时覆盖而重载可以?