当传递给Intent构造函数时,从事件处理程序传递this与ClassName.this之间有什么区别? [英] What's the difference between passing this vs. ClassName.this from an event handler when passed to Intent constructor?

查看:93
本文介绍了当传递给Intent构造函数时,从事件处理程序传递this与ClassName.this之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在较早的《 Android编程指南》(第192页)中,我们看到了LunchList#onOptionsItemSelected的实现.在此实现中,我们看到传递给startActivity的两个Intent:一个传递给构造函数的传递给LunchList.this,另一个传递给构造函数的传递给this.

In the earlier Android Programming Tutorial, page 192, we see an implementation of LunchList#onOptionsItemSelected. Within this implementation we see two Intents passed to startActivity: one whose constructor is passed LunchList.this, the other whose constructor is passed this.

有什么区别?

查看行78-91此处.请注意,如何在内部类中不声明onOptionsItemSelected.

See lines 78 - 91 here. Note how onOptionsItemSelected is not declared within an inner class.

在Android/Java中,this的值是否在事件处理程序或通过反射的函数绑定的上下文中改变?能否将两个Intent实例都传递给this?

In Android/Java does the value of this change within the context of event handlers or function binding via reflection? Could both Intent instances be passed this?

推荐答案

在Java中,this指的是包含类,而ClassName.this指的是第一个包含名称为ClassName的类.事件处理程序通常以匿名的内部类编写,因此,如果要引用事件处理程序的包含类(而不是事件处理程序的类),则需要指定ContainingClass.this,而不是this.

In Java, this refers to the containing class, and ClassName.this refers to the first containing class whose name is ClassName. Event handlers are typically written as anonymous, inner classes, so if you want to refer to the event handler's containing class (and not the event handler's class), you need to specify ContainingClass.this, not this.

参考: http://docs.oracle .com/javase/specs/jls/se7/html/jls-15.html#jls-15.8.4

这篇关于当传递给Intent构造函数时,从事件处理程序传递this与ClassName.this之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆