今天研究下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。
分享到:
相关推荐
this is a simple mysql class . join it vahdat .
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)
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 ...
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.
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, ...
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支持多文件上传的上传类 说明:支持多文件上传的上传类 ... $this->error($upload->getErrorMsg()); }else{ // 上传成功 获取上传文件信息 $info = $upload->getUploadFileInfo(); }
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, ...
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++. ///////////////////////////////////////////...
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, ...
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, ...
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++. ////////////////////////////////...
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 ...
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 ...
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 ...
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 ...
<pre name=code class=java>Toast.makeText(this,闹钟5秒后启动, Toast.LENGTH_SHORT); 用英文在google搜what‘s difference between this and Activity.this,终于有了结果(其实自己后面用了Java里ClassName....
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++. //////////////////////////////////////...