Java错误:构造函数未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18159104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:54:44  来源:igfitidea点击:

Java Error: The constructor is undefined

javaconstructor

提问by user2669883

In Java, Why am I getting this error:

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

Error: The constructor WeightIn() is undefined

Java Code:

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

I have a constructor defined.

我定义了一个构造函数。

回答by Prasad Kharkar

Add this to your class:

将此添加到您的课程中:

public WeightIn(){
}
  • Please understand that default no-argument constructor is provided only if no other constructor is written
  • If you write any constructor, then compiler does not provided default no-arg constructor. You have to specify one.
  • 请理解默认的无参数构造函数只有在没有编写其他构造函数时才提供
  • 如果您编写任何构造函数,则编译器不会提供默认的无参数构造函数。你必须指定一个。

回答by ihsan kocak

You do not have the constructor WeightIn() .Create it or give parameters in main method to constructor.

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

回答by Rahul Tripathi

WeightIn weight1 = new WeightIn();  

The default constructor is not defined. Please define it like this:-

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

public weightIn()
    {
    }

回答by Ruchira Gayan Ranaweera

In this you can't do WeightIn weight1 = new WeightIn();since default constructor is not defined.

WeightIn weight1 = new WeightIn();由于未定义默认构造函数,因此您不能这样做。

So you can add

所以你可以添加

public WeightIn(){
}

Or you can do this

或者你可以这样做

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

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

回答by spencer7593

The compiler is encountering a call to a "WeightIn()" no argument constructor, on this line:

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

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

The compiler is looking for a matching constructor in the class definition, and its not finding it. That's the error. (You do have a constructor defined: "WeightIn(double,double)" but that takes two arguments, and is not match.)

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

Several ways to fix this.

有几种方法可以解决这个问题。

The easiest is to change the code in your main method to pass two arguments.

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

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

The calls to the setWeightand setHeightmethods are redundant, since the members are already assigned values by the constructor method.

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

回答by Prajwal Sharma

First of all, you should know that one .java file can have only one public class.

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

You are getting error because you have written parameterised constructor and accessing a default constructor. To fix this error write:

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

WeightIn weight1 = new WeightIn(5.2, 52.2); 

instead of

代替

WeightIn weight1 = new WeightIn();

回答by ??? ???? ????

In Java, Why am I getting this error:

    Error: The constructor WeightIn() is undefined

It's simply because you didn't have the matching constructor for your class:

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

public class WeightIn {
  ...

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

  ...
}

you need to add the public WeightIn() {}.

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

In Java, the default constructor is automatically generated by the compiler if you didn't defined it. So, when you define the following class:

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

compiler will generating a matching default constructor for you. So, your class is implicitly having a default constructor like this:

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

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

when you instantiate the class with:

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

WeightIn weight = new WeightIn(); 

everything is alright.

一切正常。

But when you're adding a constructor by yourself, the default constructor will not generated by the compiler. So, when you're creating the class with:

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

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

  ...
}

You won't have the default constructor (i.e public WeightIn(){}). And using the following will raise an error because you have no matching constructor:

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

 WeightIn weight = new WeightIn();

回答by Ian Fischer

It took me a while but I think I finally figured out a way for this program to work. I separated the classes into different files and renamed the weight Class to Record just so that it's more obvious instead of everything being named some variation of weight. I also added an Output class to keep the code in the main as clean and minimal as possible. I hope this is similar to what you were hoping to do.
Original Code: "Constructor"

我花了一段时间,但我想我终于找到了让这个程序运行的方法。我将这些类分成不同的文件,并将权重类重命名为 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
  }


Project: weightInApp
Package: weightInApp
Class: Record.Java


项目: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;
                }
        }


Project: weightInApp
Package: weightInApp
Class: Output.Java
This class outputs the set values to a table on the console. This can be manually altered to add more data. you could also consider tracking the date of the record and adding that to this output. you would need to add the requisite variables, getters, and setters in the Record Class for that functionality.


项目: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);
        }

}
}


Project: weightInApp
Package: weightInApp
Class: Main.Java


项目: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
}
}

Sample Output

样本输出

output sample from console

控制台输出示例