Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java重写方法问题_Java_Overriding - Fatal编程技术网

Java重写方法问题

Java重写方法问题,java,overriding,Java,Overriding,我正在做一个作业“甜点商店”,基本上我需要做的是创建甜点子类Candy,Cookie,IceCream,Sundae和Checkout。我还提供了甜品类和甜品店,我不允许修改它们 虽然我已经创建了所有这些子类,但当我在TestCheckout.java上运行它时,它将不起作用,而是在Sundae中显示getName(),而不能覆盖DessertItem中的getName() public final String getName(){ ^ over

我正在做一个作业“甜点商店”,基本上我需要做的是创建甜点子类
Candy
Cookie
IceCream
Sundae
Checkout
。我还提供了甜品类和甜品店,我不允许修改它们

虽然我已经创建了所有这些子类,但当我在TestCheckout.java上运行它时,它将不起作用,而是在Sundae中显示getName(),而不能覆盖DessertItem中的getName()

public final String getName(){
                    ^
    overriden method is final.
我将为你们提供我现在所上的所有课程

public class DessertShoppe {

    public final static double TAX_RATE = 6.5;        // 6.5%
    public final static String STORE_NAME = "M & M Dessert Shoppe";
    public final static int MAX_ITEM_NAME_SIZE = 25;
    public final static int COST_WIDTH = 6;

    public static String cents2dollarsAndCents(int cents) {
        String s = "";

        if (cents < 0) {
            s += "-";
            cents *= -1;
        }

        int dollars = cents/100;
        cents = cents % 100;

        if (dollars > 0)
            s += dollars;

        s +=".";

        if (cents < 10)
            s += "0";

        s += cents;
        return s;
    } 
}


public abstract class DessertItem {

    protected String name;

    public DessertItem() {
        this("");
    }

    public DessertItem(String name) {
        if (name.length() <= DessertShoppe.MAX_ITEM_NAME_SIZE)
            this.name = name;
        else
            this.name = name.substring(0,DessertShoppe.MAX_ITEM_NAME_SIZE);
    }

    public final String getName() {
        return name;
    }

    public abstract int getCost();
}


public class Cookie extends DessertItem{

    protected double number;
    protected double pricePerDoze;

    public Cookie(String _n, double _ppd, int _number){
        super(_n);
        pricePerDoze = _ppd;
        number = _number;
    }

    public int getCost(){
        return  (int)Math.round(number / 12 * pricePerDoze);
    }
}


public class Candy extends DessertItem{

    protected double weight;
    protected double pricePerPound;

    public Candy(String _n, double _ppp, int _w){
        //using parent's constructor with name while storing its own properties
        super(_n);
        pricePerPound = _ppp;
        weight = _w;
    }

    public int getCost(){
        return  (int)Math.round(weight * pricePerPound);
    }
}


public class IceCream extends DessertItem{

    protected int cost;

    public IceCream(String _n, int _cost){
        super(_n);
        cost = _cost;
    }

    public int getCost(){
        return  cost;
    }
}


public class Sundae extends IceCream{

    protected String topName;
    protected int topCost;

    public Sundae(String _n0, int _cost0, String _n1, int _cost1){
        //put the icecream name in icecream while putting top name and cost in a separate property
        super(_n0, _cost0);
        topName = _n1;
        topCost = _cost1;
    }

    public final String getName(){
        //return both the icecream name and the topping name
        return name + " " + topName;
    }

    public int getCost(){
        //return the sum of the icecream and the topping
        return cost + topCost;
    }
}


public class Checkout{

    protected int size;
    protected DessertItem[] dessertItems;
    protected int amount;
    protected int sum;
    protected final double taxRate;

    Checkout(){
        size = 100;
        dessertItems = new DessertItem[size];
        amount = 0;
        sum = 0;
        taxRate = DessertShoppe.TAX_RATE;
    }

    public void enterItem(DessertItem d){
        dessertItems[amount] = d;
        amount ++;
    }

    public int numberOfItems(){
        return amount;
    }

    public int totalCost(){
        //make sum into zero, and calculate price from every item
        sum = 0;
        for(int i = 0; i < amount; i ++){
            sum += dessertItems[i].getCost();
        }
        return sum;
    }

    public int totalTax(){
        //use the totalCost method
        return (int)(Math.round(this.totalCost() * taxRate / 100));
    }

    public void clear(){
        //clear the array
        for(DessertItem d : dessertItems){
            d = null;
        }
        amount = 0;
        sum = 0;
    }

    public String toString(){
        String result = "Thank You! \n";

        result += DessertShoppe.STORE_NAME + "\n";

        result += "Purchased: ";

        String totalPay = DessertShoppe.cents2dollarsAndCents( totalCost()+totalTax() );
        if(totalPay.length() > DessertShoppe.COST_WIDTH){
            totalPay = totalPay.substring(0, DessertShoppe.COST_WIDTH);
        }
        result += "$" + totalPay;
        return result;
    }
}


public class TestCheckout {

    public static void main(String[] args) {

        Checkout checkout = new Checkout();

        checkout.enterItem(new Candy("Peanut Butter Fudge", 2.25, 399));
        checkout.enterItem(new IceCream("Vanilla Ice Cream",105));
        checkout.enterItem(new Sundae("Choc. Chip Ice Cream",145, "Hot Fudge", 50));
        checkout.enterItem(new Cookie("Oatmeal Raisin Cookies", 4, 399));

        System.out.println("\nNumber of items: " + checkout.numberOfItems() + "\n");
        System.out.println("\nTotal cost: " + checkout.totalCost() + "\n");
        System.out.println("\nTotal tax: " + checkout.totalTax() + "\n");
        System.out.println("\nCost + Tax: " + (checkout.totalCost() + checkout.totalTax()) + "\n");
        System.out.println(checkout);

        checkout.clear();

        checkout.enterItem(new IceCream("Strawberry Ice Cream",145));
        checkout.enterItem(new Sundae("Vanilla Ice Cream",105, "Caramel", 50));
        checkout.enterItem(new Candy("Gummy Worms", 1.33, 89));
        checkout.enterItem(new Cookie("Chocolate Chip Cookies", 4, 399));
        checkout.enterItem(new Candy("Salt Water Taffy", 1.5, 209));
        checkout.enterItem(new Candy("Candy Corn",3.0, 109));

        System.out.println("\nNumber of items: " + checkout.numberOfItems() + "\n");
        System.out.println("\nTotal cost: " + checkout.totalCost() + "\n");
        System.out.println("\nTotal tax: " + checkout.totalTax() + "\n");
        System.out.println("\nCost + Tax: " + (checkout.totalCost() + checkout.totalTax()) + "\n");
        System.out.println(checkout);
    }
}

当一个方法被标记为final时,这意味着它不能在子类中被重写

因此,您需要使用现有的
getName()
方法,并找出如何在
name
变量中获得适当的值

幸运的是,
IceCream
中有一个构造函数可以实现这一点,所以您需要做的就是将您希望
getName()
返回的内容(以及您希望
getCost()
返回的内容)传递到构造函数中:


这样,您的圣代类就不需要
getName()
getCost()
方法。

如其他答案所述,
final
修饰符意味着该方法不能被其任何子类覆盖

有两种解决方法:

第一个是将参数传递到
Sundae
构造函数中,如Jason所示:

super(_n0 + " " + _n1, _cost0 + _cost1);
第二个是利用圣代是
冰淇淋
的一个子类这一事实,圣代是
甜品
的一个子类。
名称
字段是受保护的,这意味着您可以直接从构造函数访问该字段

请记住,对于第二个选项,您需要明确检查以确保您提供的
名称
短于
甜品店.MAX\u ITEM\u name\u SIZE
,以符合该标准长度


简单且可能是预期的解决方案是第一个,尽管两者都是有效的。

Invoke
super(\u n0+“”+\u n1,\u cost0+\u cost1)的构造函数中的code>;那么你就不需要重写任何方法了。为了你自己,给你的变量起个更好的名字。这是否意味着对于圣代类,我需要写的就是你的答案?我修改了你的建议并编译了测试程序。它实际上是编译的。但是,输出显示与预期输出不同。以何种方式显示?可能构造函数的名称参数应该是
\u n1+“Sundae with\n”+\u n0
?编辑以包含此更改。签出类的
toString()
方法没有任何代码来打印单个项目。您需要在那里添加一些代码。您好,谢谢您的帮助!我使用了Jason的方法,但我编译并运行了它,它只显示了预期的输出,没有那些甜点名称和成本。。。你能解释一下为什么会这样吗?
public Sundae(String _n0, int _cost0, String _n1, int _cost1){
    super(_n1 + " Sundae with\n" + _n0, _cost0 + _cost1);
}
super(_n0 + " " + _n1, _cost0 + _cost1);