`

class .this

    博客分类:
  • java
阅读更多

   今天研究下JDK源代码,发现tools.jar中javac包下的Mian方法竟然有 Main.this..用法,百思不得其解,于是网上查找了资料,编辑了下发下。。。。

     有时候,我们会用到一些内部类和匿名类。当在匿名类中用this时(this.方法名)这个this则指的是匿名类或内部类本身。这时如果我们要使用外部类的方法和变量的话,则应该加上外部类的类名(外部类名.this.方法名)。如下面这个例子(一些代码省略了,如异常处理):  
public class Demo{  
        public Demo() {  
        Thread thread = new Thread() {  
              public void run() {  
              Demo.this.run();//调用外部类的run方法,输出 "外部类"

              System.out.println("内部类");
        };  
     this.run();//调用内部类的run方法,输出 "外部类 内部类"

     thread.start();  
}  
   public void run() {  
        System.out.println("外部类");
   }  
}  
在上面这个例子中, thread是一个匿名类对象,在它的定义中,它的run函数里用到了外部类的run函数。这时由于函数同名,直接调用就不行了。这时有两种办法,一种就是把外部的 run 函数换一个名字,但这种办法对于一个开发到中途的应用来说是不可取的。那么就可以用这个例子中的办法用 外部类的类名加上this引用来说明要调用的是外部类的方法run。 

分享到:
评论

相关推荐

    mysql.class.zip_This Is It

    this is a simple mysql class . join it vahdat .

    MICROSOFT FOUNDATION CLASS LIBRARY

    This class defines the behavior of your application's main dialog. The dialog's template is in Test.rc, which can be edited in Microsoft Visual C++. /////////////////////////////////////////////...

    Unable to load class ‘javax.xml.bind.JAXBException‘(csdn).pdf

    Unable to load class ‘javax.xml.bind.JAXBException‘(csdn)

    StringToken

    MICROSOFT FOUNDATION CLASS LIBRARY : ZSCPascal AppWizard has created this ZSCPascal application for you. This application not only demonstrates the basics of using the Microsoft Foundation classes ...

    基于wince 的嵌入式蓝牙通信

    BtWnd.h This is the main header file for... This class defines the behavior of your application's main dialog. The dialog's template is in BtWnd.rc, which can be edited in Microsoft Developer Studio.

    xLAN(获取局域网IP、MAC、用户名)

    This class defines the behavior of your application s main dialog. The dialog s template is in xLANInfo.rc, which can be edited in Microsoft Visual C++. Other standard files: StdAfx.h, ...

    仿msn(网易泡泡)界面

    This class defines the behavior of your application s main dialog. The dialog s template is in CNIcq.rc, which can be edited in Microsoft Visual C++. Other standard files: StdAfx.h, StdAfx....

    千年封包解密库

    This class defines the behavior of your application s main dialog. The dialog s template is in SubPacket.rc, which can be edited in Microsoft Visual C++. Other standard files: StdAfx.h, ...

    UploadFile.class.php支持多文件上传的上传类

    UploadFile.class.php支持多文件上传的上传类 说明:支持多文件上传的上传类 ... $this->error($upload->getErrorMsg()); }else{ // 上传成功 获取上传文件信息 $info = $upload->getUploadFileInfo(); }

    DVR-MPEG4编码播放器

    This class defines the behavior of your application s main dialog. The dialog s template is in Mp4Player.rc, which can be edited in Microsoft Visual C++. Other standard files: StdAfx.h, ...

    VC 点对点文件传输简易示例

    This class defines the behavior of your application's main dialog. The dialog's template is in Client.rc, which can be edited in Microsoft Visual C++. ///////////////////////////////////////////...

    ip包分析

    This class defines the behavior of your application s main dialog. The dialog s template is in ReadIP0802.rc, which can be edited in Microsoft Visual C++. Other standard files: StdAfx.h, ...

    win2k下用winsock原始套接字抓包

    This class defines the behavior of your application s main dialog. The dialog s template is in WS232LIB.rc, which can be edited in Microsoft Visual C++. Other standard files: StdAfx.h, ...

    网络管理 snmp manager

    This class defines the behavior of your application s main dialog. The dialog s template is in SnmpManager.rc, which can be edited in Microsoft Visual C++. ////////////////////////////////...

    Professional ASP.NET 2.0 Server Control and Component Development.part05.rar

    components that directly or indirectly derive from the ASP.NET Control base class. This group includes security controls, tabular and hierarchical data source controls such as SqlDataSource and ...

    Professional ASP.NET 2.0 Server Control and Component Development.part04.rar

    components that directly or indirectly derive from the ASP.NET Control base class. This group includes security controls, tabular and hierarchical data source controls such as SqlDataSource and ...

    Professional ASP.NET 2.0 Server Control and Component Development.part03.rar

    components that directly or indirectly derive from the ASP.NET Control base class. This group includes security controls, tabular and hierarchical data source controls such as SqlDataSource and ...

    Android代码-Codeview

    Codeview ...This library was inspired and is working thanks to highlight.js. Apps using Codeview library. https://play.google.com/store/apps/details?id=com.protectsoft.javatutorial ...

    Android this与Activity.this的区别

    <pre name=code class=java>Toast.makeText(this,闹钟5秒后启动, Toast.LENGTH_SHORT);  用英文在google搜what‘s difference between this and Activity.this,终于有了结果(其实自己后面用了Java里ClassName....

    可以用来屏蔽热键的VC源代码

    This class defines the behavior of your application's main dialog. The dialog's template is in ShieldPower.rc, which can be edited in Microsoft Visual C++. //////////////////////////////////////...

Global site tag (gtag.js) - Google Analytics