请向我解释"这个"

Pse*_*328 4 java android this

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

我正在寻找一个很好的解释"这个",我可以添加到我的笔记.任何和所有的帮助表示赞赏.提前致谢.

示例代码从下面开始:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Kaz*_*ara 17

this是指当前Object的参考.

阅读本文以获得更多理解.

举一个链接的例子:

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

    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,从区分xPointx的说法,你需要告诉编译器的区别.你实现了这一点this.意思是,当我写作时,this.x它意味着,特定x属于当前Object,在这种情况下Point.

以您提供的代码为例:

AlertDialog.Builder(this)
Run Code Online (Sandbox Code Playgroud)

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