[英]meaning of invoking default constructor
I have a homework already done, my teacher get back to me said I need to invoking the default constructor, I am not quite understand what is the difference, can anyone help to explain it to me?我已经完成了作业,我的老师回复我说我需要调用默认构造函数,我不太明白有什么区别,有人可以帮我解释一下吗? Thanks谢谢
here is my original code这是我的原始代码
public Triangle()
{
this.side1 = 1.0;
this.side2 = 1.0;
this.side3 = 1.0;
}
and the code that my teacher want me to change以及老师希望我更改的代码
public Triangle()
{
this(1.0,1.0,1.0);
}
Assuming that you already have a constructor like this:假设您已经有一个这样的构造函数:
public Triangle(double s1, double s2, double s3) {
this.side1 = s1;
this.side1 = s1;
this.side1 = s1;
}
then your two examples are functionally identical.那么你的两个例子在功能上是相同的。 However it is always good practice to call this constructor instead of repeating yourself with another three assignments.然而,调用这个构造函数而不是用另外三个赋值重复自己总是一个好习惯。
As a side note, "invoking the default" constructor is referring to invoking the constructor with no parameters, either through creating an instance or calling this()
through another constructor.作为旁注,“调用默认”构造函数是指通过创建实例或通过另一个构造函数调用this()
来调用不带参数的构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.