简体   繁体   English

无参数构造函数和带参数的构造函数之间的关系

[英]The relationship between a no-arg constructor and a constructor with arguments

Please have a look at the following class: 请看以下课程:

public class Loan {

    private double annualInterestRate;
    private int numberOfYears;
    private double loanAmount;
    private java.util.Date loanDate;

    // Default constructor
    public Loan() {
        this(2.5, 1, 1000);
    }

}

For the codeline this(2.5, 1, 1000); 对于代码行this(2.5, 1, 1000); 2.5,1,1000 this(2.5, 1, 1000); , I get the following error message in Eclipse: "The constructor Loan(double, int, int) is undefined". ,我在Eclipse中收到以下错误消息:“构造函数Loan(double,int,int)未定义”。 This error disappears when adding a new constructor with arguments: 当添加带有参数的新构造函数时,此错误消失:

// Construct a loan with specified annual interest rate, number of years and loan amount
    public Loan(double annualInterestRate, int numberOfYears, int loanAmount) {
        this.annualInterestRate = annualInterestRate;
        this.numberOfYears = numberOfYears;
        this.loanAmount = loanAmount;
        loanDate = new java.util.Date();
    }

Why does the creation of the constructor with arguments remove the "undefined" error from the default constructor? 为什么使用参数创建构造函数会从默认构造函数中消除“未定义”错误? How are these two different constructors related to each other? 这两个不同的构造函数如何相互关联?

How do I know that the values in this(2.5, 1, 1000) are assigned to the right data fields? 我怎么知道this(2.5, 1, 1000)已分配给正确的数据字段? I assume 2.5 should be assigned to annualInterestRate , 1 to numberOfYears , and 1000 to loanAmount . 我假设应该将2.5分配给annualInterestRate ,将1分配给numberOfYears ,将1000分配给loanAmount

In your no-arg constructor, the line 在您的无参数构造函数中,该行

        this(2.5, 1, 1000);

explicitly means "call another constructor for the same class as the current constructor, but which takes these arguments". 显式表示“为与当前构造函数相同的类调用另一个构造函数,但是需要这些参数”。

So that's why adding the other constructor fixes the problem. 这就是为什么添加其他构造函数可以解决此问题的原因。 The order in which you pass the arguments needs to match the order in which the parameters appear on that constructor, that constructor's parameter list defines the order you need to put the arguments in when calling it. 传递参数的顺序必须与参数在该构造函数上的显示顺序匹配,该构造函数的参数列表定义了调用参数时放入参数的顺序。

The relationship between these two constructors is that they are chained. 这两个构造函数之间的关系是它们被链接在一起。 The one with the 3 arguments is the primary constructor, the other calls the primary constructor with default values. 一个带有3个参数的参数是主要构造函数,另一个则使用默认值调用主要构造函数。 Designing constructors in this way helps to initialize your objects consistently, because a single primary constructor always gets called. 以这种方式设计构造函数有助于一致地初始化对象,因为始终会调用单个主构造函数。 (For instance, the loanDate instance field gets set regardless of which constructor you call.) (例如,无论您调用哪个构造函数,都将设置loanDate实例字段。)

You got the error 你有错误

"The constructor Loan(double, int, int) is undefined" “未定义构造函数Loan(double,int,int)”

Because if you take a look at: 因为如果您看一下:

this(2.5, 1, 1000);

You can see that the arguments are as follow 2.5 which is a double , 1 which is an int and 1000 which is also an int , so it calls a constructor with these arguments and there was no such constructor defined . 您可以看到参数如下: 2.5是一个double1是一个int1000也是一个int因此它使用这些参数调用构造函数,并且没有定义此类构造函数

And with this keyword we are calling a constructor of the current class , so we are expecting a constructor with these arguments, that's why you neeed to implement this constructor: 并使用关键字调用当前类的构造函数 ,因此我们期望具有这些参数的构造函数,这就是为什么您需要实现此构造函数的原因:

// Construct a loan with specified annual interest rate, number of years and loan amount
public Loan(double annualInterestRate, int numberOfYears, int loanAmount) {
    this.annualInterestRate = annualInterestRate;
    this.numberOfYears = numberOfYears;
    this.loanAmount = loanAmount;
    loanDate = new java.util.Date();
}

And as stated by David in comments the error disappeared because you defined the stated constructor. 正如David在评论中所述,由于您定义了所述构造函数,错误消失了。

In order to answer your query the first thing is that Java is a strongly typed language. 为了回答您的查询,第一件事是Java是一种强类型语言。 Refere Wiki 推荐维基

Now coming to your query, when you right this(2.5, 1, 100) , then it tries to find out the constructor referred by this() with the arguments types double,int,int (this is acheived in Java using implicit type conversions) in the sequence. 现在进入您的查询,当您正确this(2.5, 1, 100) 2.5,1,100 this(2.5, 1, 100) ,它将尝试找出this()引用的构造函数,其参数类型为double,int,int (这是在Java中使用隐式类型转换实现的) )。 As the sequence of the arguments matters along with type matching so it is assured that the right value is assigned to right argument. 由于参数的顺序与类型匹配很重要,因此可以确保将正确的值分配给正确的参数。 I hope it answers your queries. 希望它能回答您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM