你需要的三个明显的情况:
>调用与构造函数的第一部分相同的类中的另一个构造函数
>区分局部变量和实例变量(无论是在构造函数还是其他方法中)
>将当前对象的引用传递给另一种方法
以下是三个例子:
public class Test
{
int x;
public Test(int x)
{
this.x = x;
}
public Test()
{
this(10);
}
public void foo()
{
Helper.doSomethingWith(this);
}
public void setX(int x)
{
this.x = x;
}
}
我相信还有一些奇怪的情况,使用内部类,你需要super.this.x,但是应该避免它们非常晦涩,IMO 🙂
编辑:我不能想到任何一个例子,为什么你想要一个直的this.foo()方法调用.
编辑:saua在这个晦涩难懂的内部类的例子中作出了贡献:
I think the obscure case is: OuterClass.this.foo() when accessing foo() of the outer
class from the code in an Inner class that has a foo() method as well.