Android Activity - 是否是最后一堂课?

发布于 2024-12-18 00:24:52 字数 586 浏览 0 评论 0原文

根据这个SO问题和性能基准

我们可以假设将类定义为 public final class 可以更好地提高性能。

我们是否应该对 Android 活动做同样的事情,因为它们大多不习惯子类化?

public final class LoginActivity extends Activity { ... }

According to this SO question and performance benchmarks

We can assume that defining classes as public final class is better for performance.

Should we do the same with Android Activities, as they are mostly not used to subclassing ?

public final class LoginActivity extends Activity { ... }

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

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

发布评论

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

评论(2

貪欢 2024-12-25 00:24:52

将所有类设置为抽象类或最终类通常是一个安全的选择。

这样,一个类是否需要设计继承就一目了然了。

如果您后来发现由于某种原因需要从具体(非抽象)类继承 - 这通常表明您的整体设计存在一些不对称 - 您仍然可以删除 Final 关键字并确保你的类适合继承。

因此,通过这样做,您可以消除必须设计所有继承类的浪费,同时您可以获得具有明确意图的可维护代码。

It's usually a safe bet to make all your classes either abstract or final.

This way, it's clear whether a class needs to be designed for inheritance.

If you later find out that for some reason you need to inherit from a concrete (non-abstract) class - which is usually a sign that there's a bit of asymmetry in your overall design - you can still remove the final keyword and make sure that your class is fit for inheritance.

So by doing this you eliminate the waste of having to design all your classes for inheritance, and at the same time you get maintainable code with clear intentions.

大海や 2024-12-25 00:24:52

我从未见过任何人,包括谷歌,用代码来做到这一点。我们被告知 Dalvik VM 本身经过高度优化,因此可以得出结论,费心声明您的 Activities Final 不会导致任何明显的性能提升。声明一个类 final 通常是出于性能原因以外的原因。

I've never seen anyone, including Google, doing this in code. We're told Dalvik VM is highly optimized in itself and it can follow from this that bothering about declaring your Activities final will not result in any noticeable performance increase. One declares a class final usually for reasons other than performance reasons.

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