为什么是密封和单例?
Possible Duplicate:
Purpose of final and sealed
Hi all,
When should we opt for designing a sealed class?
When should we opt for designing a singleton class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个类的两个不相关的用途。
当您知道在执行期间将只有一个类实例时,应该使用单例。
密封类仅仅意味着它不能被子类化(或继承)。我不知道什么时候将课程标记为密封是个好时机,所以我从不这样做。
These are two unrelated uses of a class.
A singleton should be used when you know that you will have exactly one instance of the class during its execution.
A sealed class simply means that it cannot be subclassed (or inherited). I don't know when it is good situation to mark a class as sealed, so I never do.