对“这个”的澄清Java中的关键字

发布于 2024-11-18 15:26:45 字数 522 浏览 3 评论 0原文

我从 Android 开发者网站复制了这段代码:

public class ExampleActivity extends Activity implements OnClickListener {
    protected void onCreate(Bundle savedValues) {
        ...
        Button button = (Button)findViewById(R.id.corky);
        button.setOnClickListener(this);
    }

    // Implement the OnClickListener callback
    public void onClick(View v) {
        // do something when the button is clicked
    }
    ...
}

我想知道“this”关键字到底指的是什么?它是否引用“ExampleActivity”类?一般来说,如何找到“this”所指的内容?

I have this code copied from Android developers website:

public class ExampleActivity extends Activity implements OnClickListener {
    protected void onCreate(Bundle savedValues) {
        ...
        Button button = (Button)findViewById(R.id.corky);
        button.setOnClickListener(this);
    }

    // Implement the OnClickListener callback
    public void onClick(View v) {
        // do something when the button is clicked
    }
    ...
}

I am wondering what exactly "this" keyword refers to? Does it refer to the class "ExampleActivity"? And in general how to find what "this" refers to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

感性不性感 2024-11-25 15:26:45

它指的是已调用 onCreate()ExampleActivity 实例。

一般来说,来自 Java 语言规范,15.8.3

关键字 this 只能在实例方法、实例初始值设定项或构造函数的主体中使用,或者在类的实例变量的初始值设定项中使用。如果它出现在其他地方,则会发生编译时错误。

当用作主表达式时,关键字 this 表示一个值,该值是对调用实例方法的对象(第 15.12 节)或正在构造的对象的引用。 this 的类型是关键字 this 所在的类 C。在运行时,所引用的实际对象的类可能是类 C 或 C 的任何子类。

It refers to the instance of ExampleActivity on which onCreate() has been called.

In general, from the Java Language Specification, 15.8.3:

The keyword this may be used only in the body of an instance method, instance initializer or constructor, or in the initializer of an instance variable of a class. If it appears anywhere else, a compile-time error occurs.

When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked (§15.12), or to the object being constructed. The type of this is the class C within which the keyword this occurs. At run time, the class of the actual object referred to may be the class C or any subclass of C.

惟欲睡 2024-11-25 15:26:45

this 指的是最内部类实例。在您的示例中,它引用 ExampleActivity ,其类型为 OnClickListener ,并传递给 setOnClickListener

this refers to the most inner class instance. In your example it refers to ExampleActivity which is of type OnClickListener which is passed in to setOnClickListener.

墟烟 2024-11-25 15:26:45

在实例方法或
构造函数,this 是对
当前对象 — 其对象
正在调用方法或构造函数。
您可以参考该组织的任何成员
实例中的当前对象
方法或构造函数,使用 this

参考(来自 Sun Java 教程):

Within an instance method or a
constructor, this is a reference to
the current object — the object whose
method or constructor is being called.
You can refer to any member of the
current object from within an instance
method or a constructor by using this.

Reference (from the Sun Java Tutorial):

醉酒的小男人 2024-11-25 15:26:45

“this”是对当前对象的引用。

在您的情况下,它指的是ExampleActivity 类的实例。

http://download.oracle.com/javase/tutorial/java/javaOO /thiskey.html

"this" is a reference to the current object.

In your case, it refers to an instance of the ExampleActivity class.

http://download.oracle.com/javase/tutorial/java/javaOO/thiskey.html

花海 2024-11-25 15:26:45

是的,“this”指的是封闭类的实例。

Yes, 'this' refers to the instance of the enclosing class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文