假设我们有一个类名称 Home。有什么区别主页.this and 家庭班?它们指的是什么?
主页.this
Home.this
指的是当前实例Home
class.
该表达式的正式术语似乎是合格了这个 http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.8.4,如 Java 语言规范第 15.8.4 节中所引用。
在一个简单的课堂上,说Home.this
and 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() {
}
}
家庭班
Home.class
将返回的表示Home
类作为Class http://download.oracle.com/javase/6/docs/api/java/lang/Class.html object.
该表达式的正式术语是类字面量 http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.8.2,如 Java 语言规范第 15.8.2 节中所引用。
在大多数情况下,当人们使用反射 http://download.oracle.com/javase/tutorial/reflect/index.html,并且需要一种方法来引用类本身而不是类的实例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)