11

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

我定义了一个构造函数。

4

8 回答 8

21

将此添加到您的课程中:

public WeightIn(){
}
  • 请理解,只有在没有编写其他构造函数时才提供默认的无参数构造函数
  • 如果您编写任何构造函数,则编译器不会提供默认的无参数构造函数。您必须指定一个。
于 2013-08-10T05:59:07.160 回答
6

使用您当前的实现,您不能这样做,WeightIn weight1 = new WeightIn();因为未定义默认构造函数。

所以你可以添加

public WeightIn(){
}

或者你可以这样做

WeightIn weight1 = new WeightIn(3.65,1.7) // constructor accept two double values

于 2013-08-10T06:03:59.620 回答
3

编译器在这一行遇到对“ WeightIn()”无参数构造函数的调用:

WeightIn weight1 = new WeightIn();         //Error happens here.

编译器正在类定义中寻找匹配的构造函数,但没有找到。这就是错误。(您确实定义了一个构造函数:“ WeightIn(double,double)”,但这需要两个参数,并且不匹配。)

解决此问题的几种方法。

最简单的方法是更改​​ main 方法中的代码以传递两个参数。

WeightIn weight1 = new WeightIn( 3.65, 1.7); 
//weight1.setWeight(3.65);
//weight2.setHeight(1.7);

setWeightsetHeight方法的调用是多余的,因为成员已经由构造方法赋值。

于 2013-08-17T01:38:45.787 回答
1

您没有构造函数 WeightIn() 。创建它或将 main 方法中的参数提供给构造函数。

于 2013-08-10T06:00:01.867 回答
1
WeightIn weight1 = new WeightIn();  

未定义默认构造函数。请像这样定义它:-

public weightIn()
    {
    }
于 2013-08-10T06:00:02.157 回答
1
In Java, Why am I getting this error:

    Error: The constructor WeightIn() is undefined

这仅仅是因为你没有为你的类匹配的构造函数:

public class WeightIn {
  ...

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

  ...
}

您需要添加public WeightIn() {}.

在 Java 中,如果您没有定义默认构造或由编译器自动生成。因此,当您定义以下类时:

public class WeightIn {
  private double weight;
  private double height;

  // didn't define a constructor.
  public void setWeight(double weightIn){
    weight = weightIn;
  }
  public void setHeight(double heightIn){
    height = heightIn;
  }
}

编译器将为您生成匹配的默认构造函数。因此,您的类隐式具有这样的默认构造函数:

public class WeightIn {
  private double weight;
  private double height;

  // automatically generated by compiler
  public WeightIn() {
    // do nothing here.
  }

  // didn't define a constructor.
  public void setWeight(double weightIn){
    weight = weightIn;
  }
  public void setHeight(double heightIn){
    height = heightIn;
  }
}

当您使用以下方法实例化类时:

WeightIn weight = new WeightIn(); 

一切正常。

但是当您自己添加构造函数时,编译器不会生成默认构造函数。因此,当您使用以下命令创建课程时:

public class WeightIn {
  ...

  // this won't automatically generated by compiler
  // public WeightIn() {
  //   nothing to do here.
  //}

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

  ...
}

您将没有默认构造函数(即public WeightIn(){})。并且使用以下将引发错误,因为您没有匹配的构造函数:

 WeightIn weight = new WeightIn();
于 2019-03-22T07:09:50.287 回答
0

首先,你应该知道一个 .java 文件只能有一个公共类。

您收到错误是因为您编写了参数化构造函数并访问了默认构造函数。要修复此错误,请编写:

WeightIn weight1 = new WeightIn(5.2, 52.2); 

代替

WeightIn weight1 = new WeightIn();
于 2014-02-19T07:02:33.527 回答
0

我花了一段时间,但我想我终于找到了让这个程序工作的方法。我将这些类分成不同的文件,并将权重类重命名为 Record,这样它就更明显了,而不是所有的东西都被命名为权重的某种变化。我还添加了一个 Output 类,以使 main 中的代码尽可能简洁。我希望这与您希望做的类似。
原始代码:“构造函数”

public class WeightIn{
  private double weight;
  private double height;

  public WeightIn (double weightIn, double heightIn){
    weight = weightIn; //needs to be : this.weight = weight
    height = heightIn; //needs to be : this.height = height
  }


项目:weightInApp
包:weightInApp
类:Record.Java

package weightInApp;

        class Record 
        {
            private double weight; //declare variables
            private double height;
        Record (double weight, double height) {  //Parameterized Constructor method
                    this.weight = weight;  //this is the correct way 
                    this.height = height;
    //if you want to use weightIn and/or heightIn you have to be consistent acrosss 
    //the whole class. I decided to drop "In" off the variables for brevity.
                }
                //Getters and setters
                public double getWeight() { 
                    return weight;
                }
                public void setWeight(double weight) {
                    this.weight = weight;
                }
                public double getHeight() {
                    return height;
                }
                public void setHeight(double height) {
                    this.height = height;
                }
        }


项目:weightInApp
包:weightInApp
类:Output.Java
该类将设置值输出到控制台上的表格中。这可以手动更改以添加更多数据。您还可以考虑跟踪记录的日期并将其添加到此输出中。您需要在 Record Class 中为该功能添加必要的变量、getter 和 setter。

package weightInApp;

public class Output {
    static void output (Record weightIn1, Record weightIn2)
    {
        int count = 0;
        final Object[][] table = new Object[3][3];
        table[0] = new Object[] { "Record", "Weight", "Height"};
        table[1] = new Object[] { count +=1, weightIn1.getWeight(), weightIn1.getHeight() };
        table[2] = new Object[] { count+=1, weightIn2.getWeight(), weightIn2.getHeight() };
        for (final Object[] row : table) {
            System.out.format("%15s%15s%15s\n", row);
        }

}
}


项目:weightInApp
包:weightInApp
类:Main.Java

package weightInApp;
import weightInApp.Record; //imports methods and variables from the other classes
import weightInApp.Output;
public class  Main {

    public static void main (String [] args){  
        Record weightIn1 = new Record(0,0);  
//previous line of code creates a new Record object named weightIn1 
//previous line of code also sets both the values of weight and height to 0.
        weightIn1.setWeight(3.65); //sets weight for weightIn1
        weightIn1.setHeight(1.70);//sets Height for WeightIn1
        Record weightIn2 = new Record(0, 0); 
        weightIn2.setWeight(3.00);
        weightIn2.setHeight(1.75);
 //previous 3 lines are the same as above for weightIn1 
//except this is for a second object named weightIn2
        Output.output(weightIn1, weightIn2); //calls runs passes values to output method
}
}

样本输出

从控制台输出样本

于 2019-11-06T03:08:41.133 回答