什么时候,为什么你会封一类? [英] When and why would you seal a class?

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

问题描述

在C#和C ++ / CLI中keword 密封(或 NotInheritable 在VB)是用来保护一类形成任何继承的机会(类将为非遗传)。我知道,面向对象编程的一个特点就是继承和我觉得用密封违背了这个功能,它停止继承。 有一个例子说明了密封的好处时,重要的是用它?

In C# and C++/CLI the keword sealed (or NotInheritable in VB) is used to protect a class form any inheritance chance (the class will be non-inheritable). I know that one feature of object-oriented programing is the inheritance and I feel that the use of sealed goes against this feature, it stops inheritance. Is there an example shows the benefit of sealed and when it is important to use it?

推荐答案

1)在实现安全特性,使原来的对象不能被模拟的一类。

1) On a class that implements security features, so that the original object cannot be "impersonated".

2)一般说,最近我交换了一个人在微软,谁告诉我,他们试图限制继承到它真正做了充分意义上的地方,因为它变得昂贵的性能,明智的,如果不及时治疗。
密封关键字告诉CLR,没有一流的进一步下跌寻找方法,并提高速度。

2) More generally, i recently exchanged with a person at Microsoft, who told me they tried to limit the inheritance to the places where it really made full sense, because it becomes expensive performance-wise if left untreated.
The sealed keyword tells the CLR that there is no class further down to look for methods, and that speeds things up.

在大部分性能比较增强在市场上的工具的今天,你会发现一个复选框,将密封未继承的所有类。
不过要小心,因为如果你希望允许插件或组件发现通过MEF,你会遇到的问题。

In most perfomance-enhancing tools on the market nowadays, you will find a checkbox that will seal all your classes that aren't inherited.
Be careful though, because if you want to allow plugins or assembly discovery through MEF, you will run into problems.

这篇关于什么时候,为什么你会封一类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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