C# 和 VB 中的 Overridable 和 Override
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11554478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Overridable and Override in C# and VB
提问by RKh
In C#, overrideis enabled by default so, is there no need to explicitlydeclare a method as overridable in the base class? If so
在 C# 中,override默认情况下是启用的,因此是否不需要在基类中显式声明方法为可覆盖?如果是这样的话
- Is Overridable just limited to VB.NET or it is required in C# as well?
- Hence which type of methods can be overridden? Is it only abstract methods of an abstract class or any method?
- Overridable 是仅限于 VB.NET 还是在 C# 中也是必需的?
- 因此,哪些类型的方法可以被覆盖?它只是抽象类的抽象方法还是任何方法?
采纳答案by Guffa
The Overridablekeyword in VB corresponds to the virtualkeyword in C#.
的Overridable在VB关键字对应于virtual在C#关键字。
You have to make a method virtual to be able to override it. Abstract methods are automatically virtual.
您必须使方法成为虚拟方法才能覆盖它。抽象方法是自动虚拟的。
回答by Glenn Ferrie
In C#, any method marked as 'virtual' can be overridden. Methods marked as 'abstract' are not necessarily overridden, they are implemented in classes that implement the abstract class. They can be marked as virtual in the implementation. There is no limit to the number of times a virtual method can be overridden.
在 C# 中,任何标记为“虚拟”的方法都可以被覆盖。标记为“抽象”的方法不一定被覆盖,它们在实现抽象类的类中实现。它们可以在实现中标记为虚拟。虚拟方法可以被覆盖的次数没有限制。
Do you need an answer for VB.NET?
您需要 VB.NET 的答案吗?

