目的最终和密封 [英] Purpose of final and sealed

查看:144
本文介绍了目的最终和密封的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

推荐答案

根据维基百科,密封类主要用于防止派生,在编译期间增加了另一个严格级别,提高内存使用率,并触发某些可以提高运行时效率的优化。

解决方案

另外,从

According to Wikipedia, "Sealed classes are primarily used to prevent derivation. They add another level of strictness during compile-time, improve memory usage, and trigger certain optimizations that improve run-time efficiency."


  • 版本控制:当一个类最初被封装时,它可以在将来变为未密封,而不会破坏兼容性。 (...)

  • Versioning: When a class is originally sealed, it can change to unsealed in the future without breaking compatibility. (…)

性能:(...)如果JIT编译器使用密封类型查看对虚拟方法的调用,则JIT编译器可以生成更有效的代码(...)

Performance: (…) if the JIT compiler sees a call to a virtual method using a sealed types, the JIT compiler can produce more efficient code by calling the method non-virtually.(…)

安全性和可预测性:类必须保护自己的状态,并且不允许自己被破坏。当一个类被打开时,派生类可以访问和操作基类的状态,如果任何数据字段或内部操作字段的方法可以访问而不是私有的(...)

Security and Predictability: A class must protect its own state and not allow itself to ever become corrupted. When a class is unsealed, a derived class can access and manipulate the base class’s state if any data fields or methods that internally manipulate fields are accessible and not private.(…)


这些都是非常好的理由 - 我实际上并没有意识到性能优势的影响,直到我刚刚看到: p>

版本控制和安全点在代码信心方面似乎是一个巨大的好处,这对任何一种大型项目都是非常有道理的。当然,这并不意味着单元测试,但是这将有助于您。

The versioning and security points seem like a huge benefit in terms of code confidence, which is very well justified on any kind of large project. It's no drop-in for unit testing, of course, but it would help.

这篇关于目的最终和密封的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆