这个指针在 java
This pointer in java
我是 java 和 android 的新手,想了解 this
的工作原理?
public class MainActivity extends AppCompatActivity {
private class MyThread implements Runnable
{
@Override
public void run()
{
MainActivity.this.runOnUiThread(new Runnable()) {
@Override
public void run()
{
loadingSection.setVisibility(View.VISIBLE);
}
});
downloadImageUsingThreads(url);
}
}
}
什么是 MainActivity.this
?
MainActivity
是一个 class,那么 MainActivity.this
是如何工作的?
Java 没有指针。但是,Object
(s) 是通过引用实现的。
在您的示例中,您有一个内部 class (MyThread
)。 MyThread
正在访问外部 class (MainActivity
) 的实例,它在其中被 实例化。
What is MainActivity.this?
关键字 this
表示您将 class、
的实际实例作为参数传递
哪个 class?: MainActivity class
MyThread
是 MainActivity
的 inner class,因此 MyThread
的实例与 MainActivity
.
的实例相关联
对于MyThread
代码,this
指的是MyThread
的实例。要访问 MainActivity
的实例,您必须通过编写 MainActivity.this
.
来限定它
class A {
int fieldA;
class B {
int fieldB;
void methodB() {
// This method can be executed for an instance of B,
// so it can access the fields of B.
this.fieldB = 13;
// Since B is a non-static nested class of A,
// it can also access the fields is A
A.this.fieldA = 66;
}
}
void methodA() {
// This method can be executed for an instance of A,
// so it can access the fields of A.
this.fieldA = 42;
// In order to create an instance of class B, you need
// and instance of class A. This is implicitly done here.
new B(); // The new B object is associated with the A
// object defined by 'this'.
}
}
我是 java 和 android 的新手,想了解 this
的工作原理?
public class MainActivity extends AppCompatActivity {
private class MyThread implements Runnable
{
@Override
public void run()
{
MainActivity.this.runOnUiThread(new Runnable()) {
@Override
public void run()
{
loadingSection.setVisibility(View.VISIBLE);
}
});
downloadImageUsingThreads(url);
}
}
}
什么是 MainActivity.this
?
MainActivity
是一个 class,那么 MainActivity.this
是如何工作的?
Java 没有指针。但是,Object
(s) 是通过引用实现的。
在您的示例中,您有一个内部 class (MyThread
)。 MyThread
正在访问外部 class (MainActivity
) 的实例,它在其中被 实例化。
What is MainActivity.this?
关键字 this
表示您将 class、
哪个 class?: MainActivity class
MyThread
是 MainActivity
的 inner class,因此 MyThread
的实例与 MainActivity
.
对于MyThread
代码,this
指的是MyThread
的实例。要访问 MainActivity
的实例,您必须通过编写 MainActivity.this
.
class A {
int fieldA;
class B {
int fieldB;
void methodB() {
// This method can be executed for an instance of B,
// so it can access the fields of B.
this.fieldB = 13;
// Since B is a non-static nested class of A,
// it can also access the fields is A
A.this.fieldA = 66;
}
}
void methodA() {
// This method can be executed for an instance of A,
// so it can access the fields of A.
this.fieldA = 42;
// In order to create an instance of class B, you need
// and instance of class A. This is implicitly done here.
new B(); // The new B object is associated with the A
// object defined by 'this'.
}
}