在Java中,为什么会出现此错误:

Error: The constructor WeightIn() is undefined

Java代码:
public class WeightIn{
  private double weight;
  private double height;

  public WeightIn (double weightIn, double heightIn){
    weight = weightIn;
    height = heightIn;
  }
  public void setWeight(double weightIn){
    weight = weightIn;
  }
  public void setHeight(double heightIn){
    height = heightIn;
  }
}

public class WeightInApp{
  public static void main (String [] args){
    WeightIn weight1 = new WeightIn();         //Error happens here.
    weight1.setWeight(3.65);
    weight2.setHeight(1.7);
  }
}

我定义了一个构造函数。

最佳答案

将此添加到您的类(class):

public WeightIn(){
}
  • 请理解,仅当未编写其他构造函数时,才提供默认的无参数构造函数
  • 如果您编写任何构造函数,则编译器不会提供默认的no-arg构造函数。您必须指定一个。
  • 10-04 23:37