构造函数错误(实际和形式参数列表的长度不同)?

时间:2015-05-14 21:46:46

标签: constructor

我无法弄清楚为什么它会给我一个错误代码no suitable constructor found for

RegularPie(String,String,String,String,String,String,Double)
super(theName, theCrust, theSize, theSauce, theCheese, theTopping, thePrice);
^
constructor RegularPie.RegularPie(String,String,String,String,String) is not applicable
(actual and formal argument lists differ in length)
constructor RegularPie.RegularPie() is not applicable
(actual and formal argument lists differ in length)
Process completed.

怎么了?

public class RegularPie
{

    private String name;
    private String size;
    private String crust;
    private String sauce;
    private String cheese;


    public RegularPie()
    {
        name     = "regular pie";
        crust    = "Hand- Tossed";
        size     = "medium";
        sauce    = "marinara";
        cheese = "mozzarella";
    }

    public RegularPie(String theName, String theCrust, String theSize, String theSauce, String theCheese)
    {
        if(theName ==null ||theCrust ==null ||theSize ==null ||theSauce ==null ||theCheese ==null)
        {
            System.out.println("Pizza not created");
            System.exit(0);
        }
        name     = theName;
        crust    = theCrust;
        size     = theSize;
        sauce    = theSauce;
        cheese = theCheese;

    }

    public String getName()
    {
        return name;
    }
        public String getCrust()
    {
        return crust;
    }
        public String getSize()
    {
        return size;
    }
        public String getSauce()
    {
        return sauce;
    }
        public String getCheese()
    {
        return cheese;
    }
    public void setName(String newName)
    {
        if (newName == null)
        {
            System.out.println("Error with name");
            System.exit(0);
        }
        else
        {
            name = newName;
        }
    }
    public void setCrust(String newCrust)
    {
        if (newCrust == null)
        {
            System.out.println("Error with crust");
            System.exit(0);
        }
        else
        {
            crust = newCrust;
        }
    }
    public void setSize(String newSize)
    {
        if (newSize == null)
        {
            System.out.println("Error with size");
            System.exit(0);
        }
        else
        {
            size = newSize;
        }
    }
    public void setSauce(String newSauce)
    {
        if (newSauce == null)
        {
            System.out.println("Error with suace");
            System.exit(0);
        }
        else
        {
            sauce = newSauce;
        }
    }
    public void setCheese(String newCheese)
    {
        if (newCheese == null)
        {
            System.out.println("Error with topping");
            System.exit(0);
        }
        else
        {
            cheese = newCheese;
        }
    }

    public String toString()
    {
        return("You order the " + name + " pizza, that has a " + crust + " crust, a standard " + size + " size, a " + sauce +" sauce, and a " + cheese + " topping.");
    }

    public boolean equals(RegularPie otherRegularPie)
    {
        return (name.equals(otherRegularPie.name)&&crust.equals(otherRegularPie.crust)&&size.equals(otherRegularPie.size)&&sauce.equals(otherRegularPie.sauce)&&cheese.equals(otherRegularPie.cheese));
    }

    public class SpecialOne extends RegularPie
    {
        private String topping;
        private double price;

    public SpecialOne()
    {
        super();
        topping = "";
        price = 0;
    }

    public SpecialOne(String theName, String theCrust, String theSize, String theSauce, String theCheese, String theTopping, Double thePrice)
    {
        super(theName, theCrust, theSize, theSauce, theCheese, theTopping, thePrice);
        topping = theTopping;
        price = thePrice;
    }

    public SpecialOne(SpecialOne originalObject)
    {
        super(originalObject);
        topping = originalObject.topping;
        price = originalObject.price;

    }

    public String getTopping()
    {
        return topping;
    }
    public double getPrice()
    {
        return price;
    }
    public void setTopping(String newTopping)
    {
        topping = newTopping;
    }
    public void setPrice(Double newPrice)
    {
        price = newPrice;
    }

    }
}

1 个答案:

答案 0 :(得分:0)

当您致电super()时,您正在调用parrent的构造函数。因此SpecialOne调用super()会调用RegularPie的构造函数,但是您将7 Strings传递给该构造函数,而RegularPie没有接受7 Strings的构造函数}。从theTopping, thePricesuper()来电中删除SpecialOne。您在SpecialOne构造函数中明确地设置它们,因为它们是该类的成员,而不是基类的成员。