继承内隐类的问题

来源:互联网 发布:淘宝如何免费自动发货 编辑:程序博客网 时间:2024/06/10 10:31

今天想要继承android里面的一个类,但是出现了这个问题:

No enclosing instance of type WallpaperService is available due to some intermediate constructor invocation


错误代码:

import android.app.Service;import android.os.Bundle;import android.service.wallpaper.WallpaperService;import android.service.wallpaper.WallpaperService.Engine;import android.view.MotionEvent;import android.view.SurfaceHolder;public class MyEngine extends Engine{……


才发现是继承了内隐类了,查找了一下

参考引文如下:点击打开链接

这是在《JAVA编程思想》上看到的一道例题,是关于继承inner   classes(内隐类)的。 先把代码和书上的一些原话写出来,如下: 由于inner   class的构造函数必须连接到一个reference指向outer   class   对象身上,所以 当你继承inner   class时,事情便稍微复杂些。问题出在“指向outer   class对象”的那个 神秘reference必须被初始化。但derived   class之内不存在可连接的缺省对象,这个问题 的答案是,使用专用语法,明确产生该关联性: class   WithInner   {         class   Inner{} } public   class   InheritInner   extends   WithInner.Inner   {         InheritInner(WithInner   wi)   {                 wi.super();                   //---这里不懂,wi.super()指的是什么,有什么用?         }         public   static   void   main(String[]   args)   {                 WithInner   wi   =   new   WithInner();                 InheritInner   ii   =   new   InheritInner(wi);         } } 



正确修改后为:

import android.app.Service;import android.os.Bundle;import android.service.wallpaper.WallpaperService;import android.service.wallpaper.WallpaperService.Engine;import android.view.MotionEvent;import android.view.SurfaceHolder;public class MyEngine extends Engine{public MyEngine(WallpaperService wp) {//添加的wp.super();}……


原创粉丝点击