zoukankan      html  css  js  c++  java
  • JAVA中this的用法小结

    对于面对对象编程中,this指针是非常常见的。用法基本上也是差不多,这里主要记录JAVA中安卓开发方向上一个比较常见的一个特殊用法。

     1 public class Main extends Activity{
     2     @Override
     3     protected void onCreate(Bundle savedInstanceState) {
     4         super.onCreate(savedInstanceState);
     5         setContentView(R.layout.layout_main);
     6         Button button1=(Button)findViewById(R.id.button1);
     7         button1.setOnClickListener(new View.OnClickListener() {
     8             @Override
     9             public void onClick(View v) {
    10                   // 描述一个从当前对象Main跳转到helloIntentService的操作
    11                 Intent intent=new Intent(Main.this,helloIntentService.class);
    12                 // 启动上述描述的跳转
    13                 startActivity(intent);
    14             }
    15         });
    16     }
    17 }
    View Code

    上述代码中可以看到这样一句Intent intent=new Intent(Main.this,helloIntentService.class)的跳转操作,这里的Main.this只是的是Main这个对象,为什么不直接用this,因为这里有一个的内部类Intent,所以必须指明这个this是谁的引用,否则默认为当前的对象引用,即intent的引用。这样的用法在Android开发中是非常常见的,在内部类中引用外部类的时候必须指明this的归属,我暂且可以将这个this当做Object的成员变量。

  • 相关阅读:
    CodeForces 1208 A. Boys and Girls 模拟序列
    CodeForces 1209 B. Jury Size 树状数组处理区间统计问题
    Linux环境进程间通信(转IBM)
    Qt(转IBM)
    POSIX 线程详解(转IBM)
    Perl 编程系列
    Socket in Linux(转IBM)
    Return to the Basic 限定符: const和volatile.
    SQA
    (C++)从本机获取WMI数据.
  • 原文地址:https://www.cnblogs.com/hot-destiny/p/5758384.html
Copyright © 2011-2022 走看看