是C#方法密封或默认的虚拟? [英] Is C# method sealed or virtual by default?
问题描述
我知道的定义虚拟
和密封
关键字,但如果你不使用其中任何一个有的方法,可以在方法默认情况下被覆盖?
I know the definitions of virtual
and sealed
keywords, but if you don't use either of them with a method, can the method be overriden by default?
我从vb.net背景的。它是这样在vb.net(从MSDN):
I am coming from vb.net background. It goes like this in vb.net (from MSDN):
如果没有指定可重写或NotOverridable修饰符,在
默认设置取决于该属性或方法是否覆盖了一个
基类的属性或方法。如果属性或方法将重写
基类的属性或方法,默认设置为可覆盖;
否则就是NotOverridable。
If the Overridable or NotOverridable modifier is not specified, the default setting depends on whether the property or method overrides a base class property or method. If the property or method overrides a base class property or method, the default setting is Overridable; otherwise, it is NotOverridable.
我只是想知道这是否也是如此在C#。
I just want to know if that's also true in C#.
推荐答案
没有它不能。你需要明确地标记方法,虚拟允许它在派生类中重写。
No it cannot. You need to explicitly mark a method as virtual to allow it to be overridden in derived classes.
什么,但是你可以做的,是通过使用new关键字隐藏的方法。 (MSDN文档)
What you can do however, is hide a method by using the new keyword. (MSDN Documentation)
密封的关键字用来在两个类定义和方法。它不允许从方法的类或压倒一切的继承。默认情况下,如果不使用此关键字,别人就能从你的类继承。 ( MSDN文档)
The sealed keyword is used on both class definitions and methods. It disallows inheriting from a class or overriding of a method. By default, if you don't use this keyword, others will be able to inherit from your class. (MSDN Documentation)
这篇关于是C#方法密封或默认的虚拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!