MainActivity.this与this的区别

来源:互联网 发布:男生虚胖的原因知乎 编辑:程序博客网 时间:2024/05/19 14:52

<span style="font-size: 18px;">MainActivity.this与this的区别</span>
<span style="font-size: 18px;"></span>
<span style="font-size: 18px;">有时候,我们会用到一些内部类和匿名类。当在匿名类中用this时,这个this则指的是匿名类或内部类本身。这时如果我们要使用外部类的方法和变量的话,则应该加上外部类的类名。如下面例子:  例子1</span>
<span style="font-size: 18px;">public class A {  int i = 1;  public A() </span>
<span style="font-size: 18px;">{  </span>
<span style="font-size: 18px;"></span><pre class="reply-text mb10" id="content-1091089511" style="margin: 0px 0px 10px; padding: 0px; font-family: Arial; white-space: pre-wrap; -ms-zoom: 1; -ms-word-wrap: break-word; background-color: rgb(240, 240, 240);" name="code"><span style="font-size: 18px;">   public void run()</span>
<span style="font-size: 18px;">   {        System.out.println("外部类run");</span>
<span style="font-size: 18px;">  }</span>
<span style="font-size: 18px;">  Thread thread = new Thread() </span>
<span style="font-size: 18px;">  {        public void run() </span>
<span style="font-size: 18px;">      {         System.out.println("内部类run");       A.this.run();//调用外部类的run方法。</span>
<span style="font-size: 18px;">      }  };  </span>
<span style="font-size: 18px;">  this.run();//输出 “<span style="font-size: 18px;">外部类run”</span></span><span style="font-size: 18px;">  thread.start();    }  }  在上面这个例子中, thread是一个匿名类对象,在它的定义中,它的run函数里用到了外部类的run函数。这时由于函数同名,直接调用就不行了。这时有两种办法,一种就是把外部的 run 函数换一个名字,但这种办法对于一个开发到中途的应用来说是不可取的。那么就可以用这个例子中的办法用 外部类的类名加上this引用来说明要调用的是外部类的方法run。</span>
<span style="font-size: 18px;">例子2</span>
<span style="font-size: 18px;">  <span style="color: rgb(51, 51, 51); line-height: 13px; font-family: Verdana,Geneva,Arial,Helvetica,sans-serif; font-size: 13px;"><em></em></span></span><p style="margin: 5px auto; padding: 2px 0px; line-height: 20px; text-indent: 0px;">public class MainActivity extends Activity {</p><p style="margin: 5px auto; padding: 2px 0px; line-height: 20px; text-indent: 0px;">    private Button button;</p><p style="margin: 5px auto; padding: 2px 0px; line-height: 20px; text-indent: 0px;">    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);</p><p style="margin: 5px auto; padding: 2px 0px; line-height: 20px; text-indent: 0px;">        this.button = (Button) this.findViewById(R.id.Button01);        this.button.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent();                intent.setClass(MainActivity.this, NextActivity.class);                startActivity(intent);            }        });    }}</p>
<span style="font-size: 18px;">android 中实现Activity 跳转的时候 intent.setClass(Context context,Class<?> cls) </span><span style="font-size: 18px;">需要传入一个Context 参数,</span>
<span style="font-size: 18px;">这时不能用this,因为this表示的是当前对象,指的是实现OnClickListener的匿名内部类的对象,为了得到外部类<span style="line-height: 20px; font-family: Arial; font-size: 18px; white-space: pre-wrap;">MainActivity 的对象,就必须用<span style="line-height: 20px; font-family: Arial; font-size: 18px; white-space: pre-wrap;">MainActivity.this</span></span></span>
<span style="font-size: 18px;"><span style="line-height: 20px; font-family: Arial; font-size: 18px; white-space: pre-wrap;"><span style="line-height: 20px; font-family: Arial; font-size: 18px; white-space: pre-wrap;">本质上是没区别的。但是有时候必须要用MainActivity.this 这样的,比如某个控件 setOnClickListener();  在括号里面new 一个OnClickListener ,然后在onClick方法里面处理的时候必须要用MainActivity.this 而不能用this。</span></span></span>
1 0
原创粉丝点击