关于ActivityThread中使用的final关键字的一个困惑

发布于 2024-10-14 16:38:34 字数 363 浏览 5 评论 0原文

正如ActivityThread类的源代码所示,它是一个final类,并且该类中的所有方法也是final方法。由于java中的final关键字定义,该类不能被继承,但是为什么android开发者保留这些方法final呢?

可能是我问题没有表达清楚,我在这里修正一下。

ActivityThread 是一个 final 类,它不会有任何子类,也不会重写任何方法,但是你知道这个类中的所有方法都是 final ,我如果想知道为什么他们需要这些 final 关键字,他们可以删除它们而不会产生任何影响。

As the source of the ActivityThread class shows, it's a final class, and all the methods in this class is also final methods. As the final keyword definition in java, this class cannot be inherited, but why android developers keep those methods final ?

Maybe I didn't express the question clear, I fix it here.

ActivityThread is a final class, it will not have any sub-class, and no method will be overridden, but you know that all the methods in this class is final , I want to know why they need these final keywords, they can remove them with no impact.

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

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

发布评论

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

评论(2

无风消散 2024-10-21 16:38:34

Java 语言规范 清楚地表明 final 类的任何方法都不能被重写。因此,方法上的 final 声明似乎是多余的。 (也许是 Android API 测试版留下的,当时 ActivityThread 可能不是最终类?)

另一方面,优化器和混淆器有时可以使用声明为 final 的方法做更多事情。尽管他们应该足够聪明,可以推断出最终类不会有任何重写的方法,但给他们额外的提示也没什么坏处。

The Java Language Specification makes it clear that no method of a final class can be overridden. Thus, the final declaration on the methods appear to be redundant. (Maybe left over from a beta version of the Android API when ActivityThread was perhaps not a final class?)

On the other hand, optimizers and obfuscators can sometimes do a little more with methods declared final. Although they ought to be smart enough to make the inference that a final class won't have any overridden methods, it can't hurt to give them the extra hint.

黑凤梨 2024-10-21 16:38:34

<块引用>

为什么 Android 开发者将这些方法保留为最终的?

事实上,Java 工程师就是这样做的。这是一个设计决策:有时您希望禁止对某个类进行子类化。为什么?部分原因是它意味着一些强烈的责任,迫使你做出非常明智的决定。

让我参考 Effective Java 第二版 的第 17 项的部分内容约书亚·布洛赫:

<块引用>

那么,为继承而设计和记录类意味着什么?
首先,该类必须准确记录重写任何方法的效果。换句话说,该类必须记录其对可重写方法的自使用。对于每个公共或受保护的方法或构造函数,文档必须指示该方法或构造函数调用哪些可重写方法、以什么顺序以及每次调用的结果如何影响后续处理。 (可重写是指非最终的,并且是公共的或受保护的。)更一般地,类必须记录它可能调用可重写方法的任何情况。例如,调用可能来自后台线程或静态初始化程序。

...

继承设计不仅仅涉及记录自用模式。为了让程序员能够编写高效的子类而不会造成不必要的痛苦,类可能必须以明智选择的受保护方法或在极少数情况下以受保护字段的形式提供对其内部工作的挂钩。

why android developers keep those methods final?

Actually, the Java engineers did that. It's a design decision: sometimes you want to prohibit sub-classing one of your classes. Why? Some of the reason is that it implies some strong responsibility, that forces you to take very smart decisions.

Let me reference part of the item number 17 of Effective Java Second Edition by Joshua Bloch:

So what does it mean for a class to be designed and documented for inheritance?
First, the class must document precisely the effects of overriding any method. In other words, the class must document its self-use of overridable methods. For each public or protected method or constructor, the documentation must indicate which overridable methods the method or constructor invokes, in what sequence, and how the results of each invocation affect subsequent processing. (By overridable, we mean nonfinal and either public or protected.) More generally, a class must document any circumstances under which it might invoke an overridable method. For example, invocations might come from background threads or static initializers.

...

Design for inheritance involves more than just documenting patterns of self- use. To allow programmers to write efficient subclasses without undue pain, a class may have to provide hooks into its internal workings in the form of judi- ciously chosen protected methods or, in rare instances, protected fields.

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