参考“这个”在成员构造函数内
public abstract class AndroidTextAdvGame extends Activity implements Game {
Game game;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
game = this;
rightLinksListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
setScreen( new BookQuestGameScreen(game) );
}
};
}
无论如何,我可以
在新的 View.OnClickListener 中引用“this”(我定义的类)吗?
当前的解决方法是我创建的游戏成员,并在 onCreate
中分配 game=this,然后在新的 View.OnClickListener
中使用 game
public abstract class AndroidTextAdvGame extends Activity implements Game {
Game game;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
game = this;
rightLinksListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
setScreen( new BookQuestGameScreen(game) );
}
};
}
Is there anyway I can reference 'this' ( the class I defined )
in the new View.OnClickListener
?
The current workaround is that game member I created, and assigning game=this in the onCreate
and then using game in the new View.OnClickListener
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您定义的类名为 AndroidTextAdvGame,那么您可以使用以下方式引用它的“this”
If your class you defined was called AndroidTextAdvGame, then you would refer to its "this" by using
是的:您可以将外部类实例引用为
AndroidTextAdvGame.this
。例如:(我承认,一开始看起来有点奇怪,但你会习惯的。)
Yes: you can refer to the outer-class instance as
AndroidTextAdvGame.this
. For example:(It's a bit strange-looking at first, I admit, but you get used to it.)