繼承內隱類的問題

來源:互聯網
上載者:User
今天想要繼承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();}……

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.