Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可能重复: 最终目的和密封
大家好,
我们什么时候应该选择设计一个密封的类?
我们什么时候应该选择设计一个单例类?
这是类的两个不相关的用途。
当您知道在执行期间您将拥有该类的一个实例时,应该使用单例。
密封类仅仅意味着它不能被子类化(或继承)。我不知道什么时候最好将一个类标记为密封,所以我从来没有这样做过。