gpt4 book ai didi

Android - WallpaperService 为什么我的引擎必须是内部类?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:16:23 25 4
gpt4 key购买 nike

我正在制作一个简单的 Android 动态壁纸,我正在按照 Hello, Android 的第 12 章作为我的指南。

墙纸服务的基本结构如下所示:

public class MyWallpaper extends WallpaperService {

private class MyEngine extends Engine {
//...
}

//...

}

根据书MyEngine必须MyWallpaper的内部类。我没有理由对此提出异议,但这本书没有解释为什么必须如此。我不想纯粹出于风格/美学原因而使用内部类。

我想知道 MyEngine 是否真的必须是私有(private)内部类,如果是,为什么?

最佳答案

您应该这样做,因为 class Engine 嵌套在 abstract class WallpaperService 中。如果你试图让它不嵌套,你的 IDE/编译器会告诉你这样的事情:

No enclosing instance of type WallpaperService is accessible to invoke the super constructor. Must define a constructor and explicitly qualify its super constructor invocation with an instance of WallpaperService (e.g. x.super() where x is an instance of WallpaperService).

粗略地翻译一下,意思是“你可以那样做,但它最终会比你只使用嵌套类更丑陋。”

关于Android - WallpaperService 为什么我的引擎必须是内部类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3494338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com