Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

java - Please explain "this" to me

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.

example code starts below:

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

this refers to the current Object's reference.

Read this for more understanding.

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;
    }
}

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() 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...