Java错误:构造函数未定义

新手上路,请多包涵

在 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);
  }
}

我定义了一个构造函数。

原文由 user2669883 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 616
1 个回答

将此添加到您的班级:

 public WeightIn(){
}

  • 请理解,只有在没有编写其他构造函数的情况下,才会提供默认的无参数构造函数
  • 如果您编写任何构造函数,则编译器不会提供默认的无参数构造函数。你必须指定一个。

原文由 Prasad Kharkar 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题