/** * *@author androidyue *Last Modified:2011-12-11 下午4:18:07 */ public class Services { /** * 下载服务类 * @author Administrator * */ public class DownloadService{ /** * 下载文件 * @param uri 目标uri */ public void downloadFile(String uri){ System.out.println("downloading file from "+uri); } } }
/**
* Inner Classs Demo
*@author androidyue
*Last Modified:2011-12-11 下午4:11:45
*/
public class InnerClassDemo {
public static void main(String[] args){
//错误方法一
//DownloadService downloadService=new DownloadService();
//错误方法二
//DownloadService downloadService=Services.DownloadService();
//正确方法
//首先实例化内部类所在的那个外部类,这里为Services类
Services services=new Services();
//使用以下方式实力内部类,必须是类似以下这种
Services.DownloadService downloadService= services.new DownloadService();
//调用内部类对象的方法。
downloadService.downloadFile("http://www.google.com");
}
}实际上,实例化外部的类的内部类的情况不是很常见,如果遇到大家会实例化即可.
更多内容请访问http://thinkblog.sinaapp.com