当前位置: 代码迷 >> java >> Java-覆盖父类
  详细解决方案

Java-覆盖父类

热度:87   发布时间:2023-07-16 17:55:42.0

我是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);
    }
}

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

我为我的方法获得的输出比应该达到的输出领先了一步。 也就是说,例如,我的getTotal应该以1.35(我正在使用的网站输入的第一个值测试我编写的子类)开头,然后再将其减小到1.1(该网站使用员工折扣(使用构造函数中的首选布尔值),但是我的程序输出1.1,因为我的子类覆盖父类的getTotal()方法,并且从不以应有的总和开始(1.35)。 基本上,我需要知道如何从父类中获取这些原始值,然后在更改后使用重写方法来获取这些值。 如果您想了解本网站的运作方式,请以下链接访问我正在处理的问题。

附言:请让我知道是否需要提供更多/更少的信息以及我可以清理此帖子或使其更易于理解的方式。 如果我的问题太笼统,请问我您对此不了解的地方,我会尽力告诉您! 谢谢!

据我了解您的问题,您想做的是:

将此附加代码添加到DiscountBill

public class DiscountBill extends GroceryBill
{
    private static boolean isFirstTime = true;

    public double getTotal()
    {
        if (!isFirstTime && myDiscountCount > 0)
        {
           return myPrice - myDiscountAmount;
        }
        isFirstTime = false;
        return myPrice;
    }

}

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

  相关解决方案