在Java中“这个”的意思是什么?通常,我用this只适用于建筑工人。据我所知,它用于标识参数变量(通过使用this.something),如果它与全局变量具有相同的名称。但是,我不知道什么才是真正的意义this在Java中,如果我使用this无点(.).
4 回答
SMILET
TA贡献1796条经验 获得超4个赞
this
public class MyThisTest {
private int a;
public MyThisTest() {
this(42); // calls the other constructor
}
public MyThisTest(int a) {
this.a = a; // assigns the value of the parameter a to the field of the same name
}
public void frobnicate() {
int a = 1;
System.out.println(a); // refers to the local variable a
System.out.println(this.a); // refers to the field a
System.out.println(this); // refers to this entire object
}
public String toString() {
return "MyThisTest a=" + a; // refers to the field a
}}frobnicate()new MyThisTest()
1 42 MyThisTest a=42
澄清一下,您所说的是一个字段,当还有其他与字段名称相同的内容时 引用当前对象作为一个整体 在构造函数中调用当前类的其他构造函数
喵喔喔
TA贡献1735条经验 获得超5个赞
this
class Outer {
class Inner {
void foo() {
Outer o = Outer.this;
}
}}添加回答
举报
0/150
提交
取消
