(对于那些读过我上一个问题的人来说,这是同一个老师和同一个项目。)
我的老师“检查”了我的 Web 应用程序项目代码并提供了一些建议。建议之一是即使在这种情况下也使用 this
关键字:
private String getUsername() {
return username;
}
所以如果我听从他的建议,结果就会变成:
private String getUsername() {
return this.username;
}
我问他为什么,他告诉我 this 关键字除了消除歧义之外还有另一种用法。快速谷歌搜索没有返回 this 关键字的其他用法的结果。甚至是Java tutorial from Sun没有提到适合这种情况的其他用法。
最佳答案
this
还允许您从嵌套类中访问周围的类实例及其成员,例如
public class OuterClass
{
private class InnerClass
{
public OuterClass getOuter()
{
return OuterClass.this;
}
}
}
关于java - Java 中 this 关键字的其他用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1567572/