英文:
Purpose of this() in following code snippet?
问题
package test;
public class Employee {
String name;
int age;
Employee() {}
Employee(String newName, int newAge) {
this();
name = newName;
age = newAge;
}
public static void main(String args[]) {
Employee e = new Employee("N", 43);
System.out.println();
}
}
在上面的代码中,从有用性的角度来看,重载构造函数中的 "this()" 除了作为从重载构造函数调用无参构造函数的示例之外,其实际用途是什么?
英文:
package test;
public class Employee {
String name;
int age;
Employee() {}
Employee(String newName, int newAge) {
this();
name = newName;
age = newAge;
}
public static void main(String args[]) {
Employee e = new Employee("N", 43);
System.out.println();
}
}
In the above code, what is the actual point of "this()" in the overloaded constructor from a usefulness perspective besides being an example of calling the no-argument constructor from the overloaded constructor?
答案1
得分: 1
这在我看来似乎是颠倒的方式。
this() 的典型用法是在构造函数未在参数中提供默认值时,为构造函数提供默认值,因为您无法直接调用构造函数。例如,适应您的代码:
package test;
public class Employee {
String name;
int age;
Employee() {
this("Default", 42);
}
Employee(String newName, int newAge) {
name = newName;
age = newAge;
}
public static void main(String args[]) {
Employee e = new Employee();
System.out.println();
}
}
英文:
This looks like it's the wrong way round to me.
A typical use of this() would be to provide a default value for the constructor when not supplied in the argument as you cannot call the constructor directly. Adapting your code for example:
package test;
public class Employee {
String name;
int age;
Employee() {
this("Default", 42);
}
Employee(String newName, int newAge) {
name = newName;
age = newAge;
}
public static void main(String args[]) {
Employee e = new Employee();
System.out.println();
}
}
答案2
得分: 1
在确切的代码片段中?
完全没有。
任何构造函数都必须在顶部包含一个this()
调用或super()
调用。不能没有。如果你没有编写它,javac会为你插入:super();
,如果这不是有效的(例如,你的超类没有受保护的无参构造函数),那么你的代码将无法编译。
所以这就是粘贴的片段与假设的删除了this();
的片段之间的区别。展开后,你会得到以下两种情况:
展开后,不带 this():
package test;
public class Employee {
String name;
int age;
Employee() {
super(); // 调用 java.lang.Object 的无参构造函数,什么也不做。
}
Employee(String newName, int newAge) {
super(); // 调用 java.lang.Object 的无参构造函数,什么也不做。
name = newName;
age = newAge;
}
}
展开后,带 this():
package test;
public class Employee {
String name;
int age;
Employee() {
super(); // 调用 java.lang.Object 的无参构造函数,什么也不做。
}
Employee(String newName, int newAge) {
this();
name = newName;
age = newAge;
}
}
现在,在无参构造函数中插入一条 System.out.println("Hello!");
语句,现在有一个小的区别:使用 this()
,你会看到打印出Hello!
,而没有它,你就不会看到。无论哪种情况,你都会调用超类的构造函数,因为这是必须发生的,不管用哪种方式。(只有 java.lang.Object
不需要,在虚拟机中硬编码;Object
没有超类)。
英文:
In that exact snippet?
Literally nothing.
Any constructor MUST neccessarily have, at the top of it, either a this()
call, or a super()
call. You can't not. If you fail to write it, javac will inject: super();
for you, and if that isn't valid (for example, your superclass does not have a protected+ no-args constructor), then your code won't compile.
So that's the difference between the snippet as pasted and a hypothetical one where the this();
is removed. Desugaring, you get either:
Desugared, WITHOUT the this():
package test;
public class Employee {
String name;
int age;
Employee() {
super(); // invokes java.lang.Object's no-args, which does nothing.
}
Employee(String newName, int newAge) {
super(); // invokes java.lang.Object's no-args, which does nothing.
name = newName;
age = newAge;
}
}
Desugared, WITH the this():
package test;
public class Employee {
String name;
int age;
Employee() {
super(); // invokes java.lang.Object's no-args, which does nothing.
}
Employee(String newName, int newAge) {
this();
name = newName;
age = newAge;
}
}
Now, inject, say, a System.out.println("Hello!");
in the no-args constructor and now there is a small difference: With the this()
, you'd see Hello!
printed, and without it, you won't. Either way, though, you end up calling your superclass's constructor, as that is something that has to happen, one way or another. (Only java.lang.Object doesn't have to, hardcoded in the VM; Object has no superclass).
答案3
得分: 0
调用默认构造函数使用 this()。
英文:
To call the default constructor, this() is used.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论