【发布时间】:2013-09-26 22:49:19
【问题描述】:
【问题讨论】:
-
请直接在您的问题中发布代码。
-
请在问题正文中添加您的问题的简明示例,而不是使用 pastebin 链接。
-
从另一个构造函数调用的语法(构造函数委托)确实不同。使用
this(otherargs...)
标签: java object constructor
【问题讨论】:
this(otherargs...)
标签: java object constructor
您的链接示例真的很长,我对所有非英语 cmets 感到困惑,所以我只给您一个简短的示例。如果要在构造函数中调用另一个构造函数,只需使用 this 关键字。这是一个示例类,它使用 this 将“默认”(无参数)构造函数的工作委托给 1-arg 构造函数:
public class MyClass {
public final int X;
public MyClass() {
this(1); // Use X=1 by default
}
public MyClass(int x) {
X = x;
}
}
Oracle 的 Java 教程中的 Using the this Keyword: Using this with a Constructor 介绍了该技术。
【讨论】: