本文介绍了Java错误:构造函数未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,为什么会收到此错误:

In Java, Why am I getting this error:

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

我有一个构造函数。

推荐答案

将此课程添加到您的课程。

add this to your class.

public WeightIn(){
}


  • 请理解,默认的无参数构造函数

  • 如果您编写任何构造函数,那么编译器不会提供默认的无参数构造函数。您必须指定一个。

这篇关于Java错误:构造函数未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-16 21:37