为什么我不能在匿名类中使用< Class> .this? [英] Why can't I use <Class>.this in anonymous class?
问题描述
我最近使用了这段代码,并意识到在匿名类中,我无法通过.this访问实例,就像这样:
I recently use this code, and realize that in anonymous class, I can't access the instance by .this, like this:
Sprite sprFace = new Sprite() {
@Override
protected void onManagedUpdate(float pSecondElapsed) {
runOnUpdateThread(new Runnable() {
@Override
protected void run() {
Sprite.this.getParent().detach(Sprite.this); // Here
}});
}
};
我知道如何解决它(只需声明一个"me"变量),但是我需要知道为什么我不能使用<Class>.this
?
I know how to solve it (just declare a "me" variable), but I need to know why I can't use <Class>.this
?
推荐答案
相对于阴影类型,<Class>.this
语法提供了一种特殊的方式来引用类型为<Class>
的对象.
The <Class>.this
syntax gives a special way of referring to the object of type <Class>
, as opposed to the shadowing type.
此外,<Class>
必须是您尝试访问的类型的名称.在您的情况下,Sprite
不是sprFace
是Sprite
的匿名子类的实例,因此语法不适用.
In addition, <Class>
must be the name of the type you're trying to access. In your case, Sprite
is not the actual type of sprFace
. Rather, sprFace
is an instance of an anonymous subclass of Sprite
, thus the syntax is not applicable.
这篇关于为什么我不能在匿名类中使用< Class> .this?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!