为什么要上课? [英] Why seal a class?

查看:185
本文介绍了为什么要上课?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想听听.Net框架中大量密封类的动机是什么。密封课程的好处是什么?我不能理解如何不允许继承是有用的,很可能不是唯一一个打这些课程的人。



那么,为什么框架是这样设计的,而不是开创性的改变来打开一切?必须有另一个原因,但只是邪恶的?

解决方案


  • 有时候课程太贵了,要继承。

  • 运行时/反射可以在查找类型时对密码进行继承假设。一个很好的例子是 - 建议对属性进行密码查找运行速度。如果MyAttribute被密封,那么type.GetCustomAttributes(typeof(MyAttribute))将执行得更快。



该主题的MSDN文章是限制密封类的可扩展性


I'd like to hear what is the motivation behind the bulk of sealed classes in the .Net framework. What is the benefit of sealing a class? I cannot fathom how not allowing inheritance can be useful and most likely not the only one fighting these classes.

So, why is the framework designed this way and wouldn't it be unbreaking change to unseal everything? There must be another reason but just being evil?

解决方案

  • Sometimes classes are too precious and not designed to be inherited.
  • Runtime/Reflection can make inheritance assumptions about sealed classes when looking for types. A great example of this is - Attributes are recommended to be sealed for lookup runtime speed. type.GetCustomAttributes(typeof(MyAttribute)) will perform significantly faster if MyAttribute is sealed.

The MSDN article for this topic is Limiting Extensibility by Sealing Classes.

这篇关于为什么要上课?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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