请解释“这个”对我来说 [英] Please explain "this" to me

查看:99
本文介绍了请解释“这个”对我来说的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在java中阅读了关于this的数百个解释,我真的很难理解它。我正在学习android和java并排,我知道这样更难,但我很享受。我被杀的一件事就是这个......我正在粘贴下面的教程中的代码,该教程一次使用this。我只想放一段代码,但希望尽可能地提供帮助。

I've read hundreds of explanations on "this" in java and I'm really having trouble grasping it. I'm learning android and java side-by-side, I know it's harder that way but I'm enjoying it. The one thing I'm getting killed on is "this"... I'm pasting code from a tutorial below that utilizes "this" one time. I was going to just put a piece of the code but want to be as helpful as possible.

我正在寻找一个很好的解释这个,我可以添加到我的笔记。任何和所有的帮助表示赞赏。在此先感谢。

I'm looking for a good explanation of "this" that I can add to my notes. Any and all help is appreciated. Thanks in advance.

示例代码从下面开始:

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import android.view.View;
import android.content.DialogInterface;
import android.app.Dialog;
import android.app.AlertDialog;

public class DialogActivity extends Activity {
    CharSequence[] items = { "Google", "Apple", "Microsoft" };
    boolean[] itemsChecked = new boolean [items.length];

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onClick(View v) {
        showDialog(0);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case 0:
            return new AlertDialog.Builder(this)
            .setIcon(R.drawable.ic_launcher)
            .setTitle("This is a dialog with some simple text...")

            .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        Toast.makeText(getBaseContext(),
                                "OK Clicked!", Toast.LENGTH_SHORT).show();
                    }
                }
            )
            .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        Toast.makeText(getBaseContext(),
                                "Cancel clicked!", Toast.LENGTH_SHORT).show();
                    }
                }
            )
            .setMultiChoiceItems(items, itemsChecked,
                    new DialogInterface.OnMultiChoiceClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which, boolean isChecked) {
                                    Toast.makeText(getBaseContext(),
                                        items[which] + (isChecked ? " checked!":" unchecked!"),
                                        Toast.LENGTH_SHORT).show();
                    }
                }
            ).create();
        }
        return null;
    }
}


推荐答案

是指当前对象的参考。

阅读本文以获得更多理解

从链接中给出一个例子:

To give an example from the link:

public class Point {
    public int x = 0;
    public int y = 0;

    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

此处,区别于 x Point x 的参数,你需要告诉编译器区别。您可以使用来实现这一目标。这意味着,当我写, this.x 这意味着,特定的 x 属于当前的对象,在这种情况下是 Point

Here, to differentiate from the x of the Point and x of the argument, you need to tell the compiler the difference. You achieve that using this. Meaning, when I write, this.x it means, the particular x belongs to the current Object, which in the case is Point.

从代码中取示例子您提供的信息:

Taking example from the code that you have provided:

AlertDialog.Builder(this)

AlertDialog.Builder()在其构造函数中接受 Context 作为参数。但是在这里,你不做上下文someContext = new Context(); 并将其作为参数传递,因为你只需要传递你当前的活动上下文。所以你只需使用这个

AlertDialog.Builder() takes in a Context as a parameter in its constructor. But here, you don't do Context someContext = new Context(); and pass that as the parameter, because you simply need to pass your current Activity's Context. So you simply use this.

这篇关于请解释“这个”对我来说的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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