当“虚拟”是可选的时,在C#中“密封”的目的是什么 [英] What is the purpose of “sealed” in C# when “virtual” is optional

查看:74
本文介绍了当“虚拟”是可选的时,在C#中“密封”的目的是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



您可以覆盖虚拟方法或属性 密封它,以便它不能在继承层次结构中进一步覆盖。这是使用密封方法。

You can override a virtual method or property and  seal it so that it cannot be overridden further in the inheritance hierarchy. That is the use of sealed methods.


但是密封类的用法是什么。我知道密封的类不能被继承但是有什么意义呢?当我们有一个可选的虚拟。当我们不提虚拟时,派生类方法不能被覆盖吗?

But what would be the use of sealed classes.I know that sealed classes cannot be inherited but what is the point  when we have an optional virtual .When we do not mention virtual then the derived class method cannot be overriden right ?

推荐答案

我从来没有使用了密封的课程。然而, 我在基类中使用了密封方法,其中无法覆盖密封方法中的代码。代码是静态的,它将被用作/是没有方法中的代码被
覆盖和更改的可能性。

I have never used a sealed class. However,  I have used sealed methods in a base class where the code in the sealed method couldn't be overridden. The code was static and it is to be used as/is without the possibility of the code in the method being overridden and changed.

https://www.c- sharpcorner.com/article/sealed-class-in-C-Sharp /

https://www.c-sharpcorner.com/article/sealed-class-in-C-Sharp/


这篇关于当“虚拟”是可选的时,在C#中“密封”的目的是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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