复制构造函数undefined

时间:2017-11-08 22:20:12

标签: equals tostring copy-constructor

所以我正在完成创建复制构造函数的任务。 但是,程序不断给出错误。构造函数Shoes(string,double)未定义。这是什么意思以及如何解决这个问题以获得复制构造函数?

public class Shoes {


    //Instance Variables
    private String brand; // The shoes brand
    private String color; // The shoes color
    private double size;  // The shoes size


    // Assigns instance variables to default values
        public Shoes(){
            brand = "";
            color = "";
            size = 0;
        }

    //Constructor
    public Shoes (String brand, String color, double size)
    {
        this.brand = brand;
        this.color = color;
        this.size = size;
    }


    /**
     * The setBrand method stores a value in the brand field.
     * @param brand The value to store in Brand.
     */

    public void setBrand (String brand)
    {
        this.brand=brand;
    }

    /**
     * The setColor method stores a value in the color field.
     * @param color the value to store in color
     */

    public void setColor (String color)
    {
        this.color=color;
    }

    /**
     * The setSize method stores a value in the size field.
     * @param size the value to store in size
     */

    public void setSize (double size)
    {
        this.size=size;
    }

    /**
     * The getBrand method returns Shoes brand.
     * @return the value in the brand field
     */

    public String getBrand()
    {
        return brand;
    }

    /**
     * the getColor method returns Shoes color.
     * @return the value in the color field
     */

    public String getColor()
    {
        return color;
    }

    /**
     * the getSize method returns Shoes size
     * @return the value in the size field
     */

    public double getSize()
    {
        return size;
    }
    public String toString()
    {
        String str = " My shoes  " + brand + color + size; 
        return str;
    }
    public boolean equal(Shoes object2)
    {
        boolean status;
        if (brand.equals(object2.brand) &&
                size == object2.size)
                 status = true;  // Yes, the objects are equal.
              else
                 status = false; // No, the objects are not equal.

              // Return the value in status.
              return status;
    }
    public Shoes copy()
    {
        Shoes copyObject = new Shoes(brand,size);

    }
    // prints out the values of all instance variables of your object
    public void display()
    {
        System.out.println(brand);
        System.out.println(color);
        System.out.println(size);

    }


}

这是另一个帮助该程序成功运行的类。

public class Demo {

    public static void main(String[] args) {
        // Create a object
        Shoes shoes = new Shoes();

        //Call  the object's setBrand method, passing Nike as a argument.
        shoes.setBrand("Nike");

        //Call  the object's setColor method, passing Pink as a argument.
        shoes.setColor("Pink");

        //Call  the object's setSize method, passing 7 as a argument.
        shoes.setSize(7);

        shoes.display();

          // Create a Stock object.
          Shoes company1 = new Shoes("Nike",7.00);

          // Declare a Stock variable
          Shoes company2;

          // Make company2 reference a copy of the object
          // referenced by company1.
          company2 = company1.copy();

          // Display the contents of both objects.
          System.out.println("Company 1:\n" + company1);
          System.out.println();
          System.out.println("Company 2:\n" + company2);

          // Confirm that we actually have two objects.
          if (company1 == company2)
          {
             System.out.println("The company1 and company2 " +
                      "variables reference the same object.");
          }
          else
          {
             System.out.println("The company1 and company2 " +
                    "variables reference different objects.");
    }

}

这两个程序都有同样的问题,构造函数(string,double)是未定义的。我该怎么办这个错误?我真的需要帮助。

1 个答案:

答案 0 :(得分:0)

这意味着您需要添加一个带有以下参数的构造函数:

   //Constructor
    public Shoes (String brand, double size)
    {
        this.brand = brand;
        this.size = size;
    }