请向我解释“这个”

时间:2012-06-21 03:23:26

标签: java android this

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

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

示例代码从下面开始:

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

6 个答案:

答案 0 :(得分:17)

this是指当前Object的参考。

Read this for more understanding

以链接为例:

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

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

这里,要区分参数的xPoint x,需要告诉编译器区别。您可以使用this实现这一目标。这意味着,当我写作时,this.x意味着,特定的x属于当前的Object,在这种情况下是Point

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

AlertDialog.Builder(this)

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

答案 1 :(得分:2)

this视为“本身”。如果将this传递给方法,则只需将对象的实例传递给方法。

ie:Student是一个对象,Classroom也是如此。如果我想向Student添加Classroom,我可以告诉Student 本身 添加到教室(教室可以找不到学生,可以吗?)所以,我会说student.addToClassroom(new Classroom(), this);

答案 2 :(得分:2)

与其他人一样,关键字this只是对当前对象的引用。这通常是隐含的,这样如果你有一个类:

class ThisExample{
    int x;
    public ThisExample(int x){
        this.x = x;
        someMethod();
        this.someMethod();
    }

    void someMethod()
    {
    ...
    }
}

使用this.x = x有助于区分类拥有的成员变量和传递给构造函数的变量。此外,调用this.someMethod()someMethod()会完全相同,因为this是隐含的。

在Android中,有时你会看到一个传递方法,如someMethod(this)。这里发生的是this指的是当前活动的Context,这只是一堆信息,解释了活动的所有内容。

答案 3 :(得分:1)

好的,我会看到我的样子:P

将Java对象(类)视为一个单独的实体,它具有某些定义它的内容(properties)和它可以做的某些事情(methods

例如,采用名为Machine

的(非常抽象的)类
class Machine {
    Piston piston1;
    ArrayList<Gear> gears;

    public void addSomeNewGears(ArrayList<Gear> gears)
    {
        for(int i = 0; i < gears.size(); i++)
        {
            this.gears.Add(gears[i]);
        }    
    }
}

在方法addSomeNewGears中,我们实际上可以访问名为gears的两个列表:

  • 机器对象的当前档位,
  • 我们要添加的新内容。

因为它们都被称为gears,所以我们想要访问哪一个是不明确的,但是新的List将优先,因为它在方法中被声明为本地。

要访问机器的齿轮,我们需要使用this关键字,它告诉编译器我们正在寻找class的齿轮,而不是method

希望这有帮助!

答案 4 :(得分:0)

类的实例方法(未声明为static的方法)只能通过引用该类的某个实例来执行。例如:

class Foo {
    public void doSomething() {
        // "this" refers to the current object
        . . .
    }
    . . .
}

// then later:
Foo aFoo = new Foo();
aFoo.doSomething(); // "this" will be equal to "aFoo" for this call

// The following is illegal:
doSomething();

// so is this:
Foo.doSomething();

在方法doSomething()中,变量this引用用于调用方法的Foo的特定实例(在此示例中,{{1引用的当前对象) }})。

答案 5 :(得分:0)

这是当前对象的引用。这对于识别成员属于当前类非常有用。 例如, 班级样本{ int a; 样本(int a){ this.a = A; } } “this”将区分当前的类变量和其他变量