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

发布于 2024-09-14 09:13:22 字数 451 浏览 15 评论 0原文

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

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

public class MyWallpaper extends WallpaperService {

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

    //...

}

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

我想知道 MyEngine 是否实际上必须是私有内部类,如果是的话,为什么?

I'm working on a simple android live wallpaper, I'm following chapter 12 from Hello, Android as my guide.

The bare-bones of a wallpaper service looks like this:

public class MyWallpaper extends WallpaperService {

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

    //...

}

According to the book MyEngine must be an inner class of MyWallpaper. I have no reason to dispute this, but the book offers no explanation as to why this must be so. I prefer not to use inner classes purely for stylistic/aesthetic reasons.

I was wondering if MyEngine actually has to be a private inner class and, if so, why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

权谋诡计 2024-09-21 09:13:22

您应该这样做,因为class Engine 嵌套在抽象类WallpaperService 中。如果您尝试使其不嵌套,您的 IDE/编译器会告诉您如下信息:

没有类型的封闭实例
壁纸服务可访问
调用超级构造函数。必须
定义一个构造函数并显式地
限定其超级构造函数
调用一个实例
WallpaperService(例如xsuper(),其中
x 是 WallpaperService 的一个实例)。

粗略地翻译,这意味着“你可以这样做,但最终会比只使用嵌套类更难看”。

You're supposed to do it this way because class Engine is nested within the abstract class WallpaperService. If you try to make it not nested, your IDE/compiler will tell you something like this:

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).

Which, loosely translated, means "you could do it that way, but it's going to end up uglier than if you just use the nested class."

灯角 2024-09-21 09:13:22

你可以将你的引擎放在一个单独的类中。我刚刚使用自己的壁纸进行了尝试,它编译并运行良好。

在您的 WallpaperService 子类中的 onCreateEngine() 重写中,只需将“this”传递给您的引擎构造函数即可。构造函数应该将其作为壁纸服务接收。在构造函数的第一行中,调用 wallSvcObject.super()。

编辑:在考虑 Justin Buser 所说的内容后,我不确定我的建议是否好。您的引擎将失去对WallpaperService成员的访问,因为它都必须通过wallpaperSvcObject。我不知道他指的是不是这个。

You CAN have your Engine in a separate class. I just tried it with my own wallpaper and it compiled and runs fine.

In the onCreateEngine() override in your WallpaperService subclass, just pass 'this' to your Engine constructor. The constructor should receive it as a WallpaperService. In the first line of your constructor, make a call to wallpaperSvcObject.super().

EDIT: After thinking about what Justin Buser said, I'm not sure if my advice is good. Your Engine would lose access to members of WallpaperService because it'd all have to go through wallpaperSvcObject. I don't know if that's what he was referring to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文