coo*_*ird 61
Home.this
Home.this指的是Home该类的当前实例.
该表达式的正式术语似乎是合格的,如Java语言规范的第15.8.4节所述.
在一个简单的课堂上,说Home.this和this将是相同的.此表达式仅用于存在内部类的情况,并且需要引用封闭类.
例如:
class Hello {
class World {
public void doSomething() {
Hello.this.doAnotherThing();
// Here, "this" alone would refer to the instance of
// the World class, so one needs to specify that the
// instance of the Hello class is what is being
// referred to.
}
}
public void doAnotherThing() {
}
}
Run Code Online (Sandbox Code Playgroud)
Home.class
Home.class将返回Home类的表示形式作为Class对象.
此表达式的正式术语是类文字,如Java语言规范的第15.8.2节中所述.
在大多数情况下,当使用反射时使用此表达式,并且需要一种方法来引用类本身而不是类的实例.
Home.classjava.lang.Class<Home>返回与该类相对应的实例Home。该对象允许您反映该类(找出它有哪些方法和变量,它的父类是什么等)并创建该类的实例。
Home.this仅当您位于 Home 的嵌套类中时才有意义。这里Home.this将返回Home嵌套类的对象所嵌套的类的对象。