【问题标题】:Constructor Errors (actual and formal argument lists differ in length)?构造函数错误(实际参数列表和形式参数列表的长度不同)?
【发布时间】:2015-05-14 21:46:45
【问题描述】:

我不明白为什么它给了我一个错误代码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;
    }

    }
}

【问题讨论】:

  • 粘贴你使用RegularPie类的代码并得到错误。 RegularPie的超类是什么?
  • 普通派应该是超级派

标签: constructor


【解决方案1】:

当您调用super() 时,您正在调用parrent 的构造函数。所以SpecialOne 调用super() 调用RegularPie 的构造函数,但是您将七个Strings 传递给该构造函数,而RegularPie 没有接受七个Strings 的构造函数。从super() 调用SpecialOne 中删除theTopping, thePrice。您在 SpecialOne 构造函数中明确设置它们,因为它们是该类的成员,而不是基类的成员。

【讨论】:

    猜你喜欢
    • 2020-08-13
    • 1970-01-01
    • 2016-05-31
    • 2013-10-04
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 2015-07-25
    • 2014-04-06
    相关资源
    最近更新 更多