问。确定第 1 行用于调用无参数构造函数 Product() 的有效选项。
class Product{
int productId;
String productName;
Product( ) {
productId=0; productName="";
}
Product(int id, String name) {
//access Product() ---- Line 1
productId=id;
productName=name;
}
}
A.这个();
你能帮我理解如何在这里使用“this”引用变量吗?
最佳答案
Can you help me understand how 'this' reference variable can be used here?
this()
未使用变量。
这是一个特殊的语法结构,表示“调用同一个类中的另一个构造函数”。它称为“显式构造函数调用”(或更具体地说,“备用构造函数调用”),并且只能作为构造函数中的第一条语句出现(请参阅 language spec )。
但是,您可以稍后在构造函数中使用 this
作为“变量”,以便访问当前正在构造的实例上的成员变量或实例方法,例如:
this.productId = id;
关于java - 从另一个调用构造函数 - this 关键字用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60873739/