请解释“这”给我(Please explain “this” to me)

2019-06-25 15:19发布

我读过数百名在Java中的“this”上的解释,我真的有麻烦抓住它。 我学的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;
    }
}

Answer 1:

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

在这里,从区分x中的Pointx的说法,你需要告诉编译器的区别。 您实现,使用this 。 意思是,当我写, this.x这意味着,特定的x属于当前Object ,这在情况下Point

以从您所提供的代码示例:

AlertDialog.Builder(this)

AlertDialog.Builder()获取一个Context作为其构造函数的参数。 但在这里,你不这样做Context someContext = new Context(); 并传递作为参数,因为你只需要通过你当前ActivityContext 。 所以,你只需使用this



Answer 2:

想想this是“自己”。 如果你传递this一个方法,你只是传递对象的实例的方法。

即: Student是一个对象,如Classroom 。 如果我想添加一个StudentClassroom ,我会告诉Student自身添加到教室(教室找不到学生,可他们?)。 所以,我会说student.addToClassroom(new Classroom(), this);



Answer 3:

关键字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 ,这是只是一堆的资料,解释有关的一切活动。



Answer 4:

好吧,我会看到我是如何去: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以我们想要的一个访问,但因为它是在方法局部声明新的名单,应优先考虑也可以是不明确的。

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

希望这可以帮助!



Answer 5:

实例方法(这些未声明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在于使用要调用的方法(在此示例中,通过引用的当前对象aFoo )。



Answer 6:

这无非是当前对象的参考其他。 这将是确定的成员属于当前类是非常有用的。 例如,类样品{的INT A; 样品(INT A){this.a = A; } } "this" will differentiate current class variable and other variable }}“这”将区分当前类变量和其他变量



文章来源: Please explain “this” to me