必须继承抽象方法且隐式超级构造函数未定义

时间:2019-02-17 08:47:02

标签: inheritance abstract superclass

我正在研究一个关于继承的学校项目,但遇到两个错误:

  • Pastry类型必须实现继承的抽象方法DessertItem.getCost()
  • 隐式超级构造函数DessertItem()对于默认构造函数未定义。必须定义一个显式构造函数

    public abstract class DessertItem {
    private String name;
    
    public DessertItem(String n)
    {
        name = n;
    }
    
    public void setName(String n)
    {
        name = n;
    }
    public String getName()
    {
        return name;
    }
    
    public String toString()
    {
        return getName();
    }
    
    public abstract double getCost(); 
    

    }

    public class Pastry extends DessertItem {
    private int quantity;
    private double cost;
    private boolean heating;
    
    public Pastry(String n)
    {
        super(n);
    }
    
    public void setQuantity(int q)
    {
        quantity = q;
    }
    public int getQuantity()
    {
        return quantity;
    }
    
    public void setHeating(boolean h)
    {
        heating = h;
    }
    public boolean getHeating()
    {
        return heating;
    }
    
    public double getCost()
    {
        if(getName() == "Funnel cake")
            cost = getQuantity() * 3.00;
        else if(getName() == "Donut")
            cost = getQuantity() * 1.25;
        else if(getName() == "Muffin")
            cost = getQuantity() * 1.00;
    
        return cost;
    }
    

    }

我以为我实现了getCost(),但我不太确定构造函数有什么问题。任何帮助表示赞赏!

0 个答案:

没有答案