Java错误:构造函数未定义

时间:2013-08-10 05:56:51

标签: java constructor

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

我定义了一个构造函数。

9 个答案:

答案 0 :(得分:16)

将此添加到您的班级:

public WeightIn(){
}
  • 请理解,只有在没有编写其他构造函数时才提供默认的无参数构造函数
  • 如果编写任何构造函数,则编译器不提供默认的no-arg构造函数。你必须指定一个。

答案 1 :(得分:4)

在此,您无法执行WeightIn weight1 = new WeightIn();,因为未定义默认构造函数。

所以你可以添加

public WeightIn(){
}

或者你可以这样做

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

答案 2 :(得分:1)

你没有构造函数WeightIn()。创建它或者将main方法中的参数赋给构造函数。

答案 3 :(得分:1)

WeightIn weight1 = new WeightIn();  

未定义默认构造函数。请按如下方式定义: -

public weightIn()
    {
    }

答案 4 :(得分:1)

编译器在此行上遇到对“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方法的调用是多余的,因为已经通过构造函数方法为成员分配了值。

答案 5 :(得分:0)

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

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

WeightIn weight1 = new WeightIn(5.2, 52.2); 

而不是

WeightIn weight1 = new WeightIn();

答案 6 :(得分:0)

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中,default construct或由编译器自动生成(如果未定义)。因此,当您定义以下类时:

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

答案 7 :(得分:0)

花了我一段时间,但我想我终于找到了使该程序起作用的方法。 我将类分为不同的文件,并将权重类重命名为Record,以使其更加明显,而不是将所有内容都命名为权重的某种变化。我还添加了一个Output类,以使主代码尽可能保持干净和最小化。 我希望这与您希望执行的操作类似。
原始代码:“构造函数”

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

示例输出

output sample from console

答案 8 :(得分:-2)

错误:构造函数未定义 可能的原因:

  1. 您可能提供的参数与任何可用的构造函数都不匹配

2。您可能给了错误的数据类型参数。