“密封"关键字在现实生活中的用法 [英] Real life usage of the “sealed” keyword

查看:94
本文介绍了“密封"关键字在现实生活中的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提供sealed关键字在现实生活中的使用示例吗?谢谢.

Can some one provide a couple of examples of real life usage of the sealed keyword? Thank you.

推荐答案

密封类不能被继承,任何继承密封类的尝试都将导致错误,您需要为此做功课;

这是解释密封类的链接

密封类说明

为了简化您的工作,我已经为您搜索了

密封类现实生活中的例子


如果发现有用,别忘了标记此答案

谢谢与问候
基数:rose:
Sealed class cannot be inherited and any attempts to inherit the sealed class will result in error, you need to do homework on this;

here''s a link which explains sealed class

Sealed class explanation

and to make your work easy i have googled it for you

Sealed class real life examples


Don''t forget to mark this answer if you find it useful

Thanks and Regards
Radix :rose:


正如Toli已经回答的那样,预计sealed 类在JIT期间将具有轻微的性能优势.这并没有人们期望的那么好,因此这不是制作类sealed的决定性原因.

由于sealed 类将没有子类,因此JIT编译器可以安全地生成 call IL指令,而不是 callvirt IL指令当确定在其上进行调用的实例为非null时.因此,即使它正在调用virtual 方法,如果可以肯定地知道该对象为非null,它也可以发出 call 指令,该指令比 callvirt 指令.

再说一次,不要以此为基础来密封你的课.仅当您认为这是您的类层次结构的最佳选择时,才执行此操作.
As Toli has replied already, sealed classes are expected to have a slight performance advantage during JIT. It''s not as good a difference as people expect it to be, so that should not be the deciding reason to make a class sealed.

Since a sealed class will not have child classes, the JIT compiler can safely generate call IL instructions instead of callvirt IL instructions when it is sure that the instance on which the call is made is non-null. So even if it''s invoking a virtual method, if it knows for certain that the object is non-null, it can emit a call instruction which is faster than a callvirt instruction.

Once again, do not use this as a basis to seal your class. Do that only if you think it''s the best choice for your class hierarchy.


通常,在现实环境中,第三方代理可以使用.Net dll文件并通过创建来继承类一个新的软件,因此在这种情况下,要保护声明为密封的类和受保护的访问修饰符,Sealed类对于限制继承的层次结构也很有用.

如果有帮助,请 投票 接受答案 .
Generally in real world enviornment the third party rendors can use .Net dll files and inherit the classes by creating a new software,So in this case to protect the classes declared as a sealed and protected access modifiers,Sealed class is also useful to restrict the hierarchy of inheritance.

Please vote and Accept Answer if it Helped.


这篇关于“密封"关键字在现实生活中的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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