Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java错误:构造函数未定义_Java_Constructor - Fatal编程技术网

Java错误:构造函数未定义

Java错误:构造函数未定义,java,constructor,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;

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

我定义了一个构造函数

将此添加到您的类中:

public WeightIn(){
}
public class WeightIn {
  ...

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

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

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

WeightIn weight1 = new WeightIn();  
未定义默认构造函数。请这样定义它:-

public weightIn()
    {
    }

在这种情况下,您不能执行
WeightIn-weight1=new-WeightIn()因为未定义默认构造函数

所以你可以加上

public WeightIn(){
}
或者你可以这样做


WeightIn weight1=new WeightIn(3.65,1.7)//构造函数接受两个双值

编译器在此行遇到对“
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);

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

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

由于编写了参数化构造函数并访问了默认构造函数,所以出现了错误。要修复此错误,请写入:

WeightIn weight1 = new WeightIn(5.2, 52.2); 
而不是

WeightIn weight1 = new WeightIn();
这只是因为您的类没有匹配的构造函数:

public WeightIn(){
}
public class WeightIn {
  ...

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

  ...
}
您需要添加
公共权重()

在Java中,如果没有定义or,编译器会自动生成or。因此,当您定义以下类时:

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

  ...
}
一切都很好

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

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

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

 WeightIn weight = new WeightIn();

这花了我一段时间,但我想我终于想出了一个办法让这个程序工作。 我将这些类分为不同的文件,并将weight类重命名为Record,这样它就更明显了,而不是将所有内容都命名为weight的一些变体。我还添加了一个输出类,以尽可能保持主代码中的代码干净和最少。 我希望这和你希望做的一样。
原始代码:“构造函数”


项目:权重应用
套餐: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;
                }
        }
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
}
}

项目:权重应用
套餐:weightInApp
类:Output.Java
此类将设置的值输出到控制台上的表中。可以手动更改以添加更多数据。您还可以考虑跟踪记录的日期并将其添加到此输出中。您需要在记录类中为该功能添加必需的变量、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
类:Main.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;
                }
        }
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
}
}
样本输出


编译器是正确的。构造函数
WeightIn()
未定义。请更正代码,然后通过一份副本,以便我能理解它。rit仍然不起作用+1这是唯一一个真正澄清错误原因的答案:它不在构造函数的定义中,它是在调用一个与任何已定义的构造函数签名都不匹配的构造函数。这种见解将问题的焦点从“我没有正确地编写类构造函数”转移到“我没有正确地调用类构造函数”,这对n00b(像我一样)对问题根源的理解产生了影响。所有其他答案似乎都暗示,在重载无参数构造函数之前,必须先编写无参数构造函数,但事实并非如此。@TomAuger:你完全正确。不需要提供无参数构造函数。我们可能想这样做,但这不是必须的。(你是对的,有些答案误导性地使用了“你需要”一词,并且只提供了一个解决方案,好像这是唯一的解决方案。(每当我读到一个回答说“你需要”我总是问,这是真的吗?一个人真的需要,或者有其他更好的选择吗?@TomAuger:你也很正确,这里的许多答案跳过了被问到的问题“为什么我会犯这个错误?”并尝试回答其他问题。