请向我解释“这个”
我已经在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; } }
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; } }
这里,为了区分参数的Point
和x
,你需要告诉编译器区别。 你用它来实现这this
。 这意味着,当我写这个x
时,它意味着,特定的x
属于当前的Object
,在本例中是Point
。
以您提供的代码为例:
AlertDialog.Builder(this)
AlertDialog.Builder()在其构造函数中接受Context
作为参数。 但是在这里,你不做Context someContext = new Context();
并将其作为参数传递,因为您只需要传递当前Activity
的Context
。 所以你只需使用this
。
把它想象成“本身”。 如果this
方法传递给方法,则只需将对象的实例传递给方法即可。
即: Student
是一个对象,就像Classroom
。 如果我想在Classroom
上添加一名Student
,我可能会告诉Student
将自己添加到课堂中(课堂上找不到学生,可以吗?)。 所以,我会说student.addToClassroom(new Classroom(), this);
像其他人所说的那样,关键字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
,它只是解释活动所有内容的一堆信息。
好的,我会看到我的样子:P
将Java对象(类)视为一个单独的实体,它具有某些定义它是什么( properties
)的东西以及它可以做的某些事情( methods
)
例如,采用名为Machine
的(非常抽象的)类
class Machine { Piston piston1; ArrayList gears; public void addSomeNewGears(ArrayList gears) { for(int i = 0; i < gears.size(); i++) { this.gears.Add(gears[i]); } } }
在方法addSomeNewGears
我们实际上可以访问名为gears的两个列表:
- Machine对象的当前齿轮,
- 我们要添加的新内容。
因为它们都被称为gears
所以我们想要访问哪一个可能是模糊的,但是新的List将优先,因为它在方法中被声明为本地。
要访问Machine的齿轮,我们需要使用this
关键字,它告诉编译器我们正在寻找class
的齿轮,而不是method
的
希望这可以帮助!
只能通过引用类的某个实例来执行类的实例方法(未声明为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
引用的当前对象)。
这不过是当前对象的引用。 这对于识别成员属于当前类非常有用。 例如,Class Sample {int a; 样本(int a){this.a = a; “this”将区分当前类变量和其他变量