通过"这个"实现重载的构造函数。电话

时间:2016-10-25 08:11:28

标签: java constructor

我收到一条错误消息非静态变量这不能从静态上下文中引用parts[0] = new Part("Nut", 12, 0.05);我如何通过"这个"来实现重载的构造函数。调用

  Part[] parts = new Part[5];

  parts[0] = new Part("Nut", 12, 0.05);
  parts[1] = new Part("Bolt", 15, 0.07);
  // default name "Unknown"
  parts[2] = new Part();
  // default quantity -1
  parts[3] = new Part("Screw");
  // default price .01
  parts[4] = new Part("Grommet", 4);

  System.out.println("\nPARTS LIST");
  for(Part p : parts)
  {
     System.out.println(p.getDescription() + ", " + p.getQuantity()
        + ", " + p.getCost());
  }

  parts[0].setDescription("Hex nut");
  parts[1].setQuantity(30);
  parts[2].setCost(0.19);
  parts[3].setDescription("Rubber grommet");
  parts[4].setQuantity(40);

  System.out.println("\nPARTS LIST AFTER CHANGES");
  for(Part p : parts)
  {
     // Uses the toString method!
     System.out.println(p);
  }
   }

class Part
{
private String partDesc;
private int quantity;           // parts quantity
private double cost;            // parts cost

public Part(String d, int q, double c)  // Constructor for Part
{
    partDesc = d;
    quantity = q;
    cost = c;
}

public Part(String partDesc, double cost)
{
    this("Unknown", -1, cost);
}

public Part( String partDesc)
{
    this(partDesc, -1, .01);
}

public Part(String PartDesc, int quantity)
{
    this(partDesc, quantity, 0.1);
}

public String getDescription() // getter for parts description
{
    return partDesc;
}

public int getQuantity ()       // getter for parts quantity
{
    return quantity;
}
public double getCost()         // getter for parts cost
{
    return cost;
}

public String setDescription(String d)   //setter for parts description
{
    partDesc = d;
    return partDesc;
}
public int setQuantity(int newQuantity)         //setter for parts quantity
{
    quantity = newQuantity;
}
public double setCost(double newCost)                   //setter for parts cost
{
    cost = newCost;
}
public String toString()
{
    return partDesc + " , " + quantity + " , " + cost;
}
}

2 个答案:

答案 0 :(得分:0)

你犯了很多错误。我修好了。 默认构造函数未定义。因为这里使用默认构造函数创建对象。 某些方法不添加return语句。

我在这里添加了评论。

参考:http://beginnersbook.com/2013/05/constructor-overloading/

public class Part {

private String partDesc; 
private int quantity; // parts quantity 
private double cost; // parts cost

//Define default constructor. Because You have created object with this.
public Part()  // Constructor for Part
{

}

public Part(String d, int q, double c)  // Constructor for Part
{
    partDesc = d;
    quantity = q;
    cost = c;
}

public Part(String partDesc, double cost)
{
    this("Unknown", -1, cost);
}

public Part( String partDesc)
{
    this(partDesc, -1, .01);
}

public Part(String partDesc, int quantity)//Here p should be simple.unless show error
{
    this(partDesc, quantity, 0.1);
}



public String getDescription() // getter for parts description
{
    return partDesc;
}

public int getQuantity ()       // getter for parts quantity
{
    return quantity;
}
public double getCost()         // getter for parts cost
{
    return cost;
}

public String setDescription(String d)   //setter for parts description
{
    partDesc = d;
    return partDesc;
}
public int setQuantity(int newQuantity)         //setter for parts quantity
{
    quantity = newQuantity;
    return quantity;//Here return value
}
public double setCost(double newCost)                   //setter for parts cost
{
    cost = newCost;
    return cost;//Here return value
}
public String toString()
{
    return partDesc + " , " + quantity + " , " + cost;
}





      Part[] parts = new Part[5];

      parts[0] = new Part("Nut", 12, 0.05);
      parts[1] = new Part("Bolt", 15, 0.07);
      // default name "Unknown"
      parts[2] = new Part();
      // default quantity -1
      parts[3] = new Part("Screw");
      // default price .01
      parts[4] = new Part("Grommet", 4);

      System.out.println("\nPARTS LIST");
      for(Part p : parts)
      {
         System.out.println(p.getDescription() + ", " + p.getQuantity()
            + ", " + p.getCost());
      }

      parts[0].setDescription("Hex nut");
      parts[1].setQuantity(30);
      parts[2].setCost(0.19);
      parts[3].setDescription("Rubber grommet");
      parts[4].setQuantity(40);


      System.out.println("\nPARTS LIST AFTER CHANGES");
      for(Part p : parts)
      {
         // Uses the toString method!
         System.out.println(p);
      }

答案 1 :(得分:0)

首先,如果你需要一个参数,你应该使用它,所以,而不是

public Part(String partDesc, double cost) {
    this("Unknown", -1, cost);
}
你应该:

public Part(String partDesc, double cost) {
    this(partDesc, -1, cost);
}

"Unknown"参数应该用于您实际上不提供partDesc的情况,例如默认构造函数。

友好的建议:所有部件都有成本,所以你应该总是询问价格。但如果坚持在没有cost的情况下创建重载的构造函数,您可以简单地将它放到0.0d

所以,你可以这样:

class Part {

    private String partDesc;
    private int quantity;           // parts quantity
    private double cost;            // parts cost

    public Part() { //Default constructor
        this( "Unknown", 1, 0.0d );
    }

    public Part(String d, int q, double c)  // Constructor for Part
    {
        partDesc = d;
        quantity = q;
        cost = c;
    }

    public Part(String partDesc, double cost) {
        this(partDesc,  , 1, cost);
    }

    public Part( String partDesc) {
        this( partDesc, 1, 0.0d );
    }

    public Part(String PartDesc, int quantity) {
        this( partDesc, quantity, 0.0d );
    }

    //Rest of the class...

}

我希望我有所帮助。

祝你有愉快的一天。 :)