C#中有虚拟类吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15104119/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:03:58  来源:igfitidea点击:

Is there Virtual class in C#?

c#

提问by Tejus

I have read about virtual methods in C#. Is there any concept called virtual class in C#? Is abstract class in C# and virtual class in C++ the same?

我已经阅读了 C# 中的虚拟方法。C#中是否有任何称为虚拟类的概念?C#中的抽象类和C++中的虚拟类是一样的吗?

采纳答案by BrokenGlass

There is no such thing in C# - and it's really not necessary since multiple implementation inheritance is not supported. Making a class abstract really only means that you cannot create instances of that class and they might not be fully implemented (e.g. might contain abstract methods).

C# 中没有这样的东西 - 而且它真的没有必要,因为不支持多实现继承。使类抽象实际上仅意味着您不能创建该类的实例,并且它们可能没有完全实现(例如可能包含抽象方法)。

回答by si618

There are no virtual classes in C#. Abstract class is not the same since you cannot instantiate an abstract class.

C# 中没有虚拟类。抽象类不一样,因为您不能实例化抽象类。

You can do the opposite of marking something virtual by marking it sealed, which prevents it from being inherited.

您可以通过标记它做标记的东西虚拟的相反密封,以防止它被继承。

回答by Isaiah Nelson

I will opine with a link to this earlier SO post: In C++ what is a virtual base class?

我将通过链接到此较早的 SO 帖子:在 C++ 中什么是虚拟基类?

As others have mentioned, since C# does not have multiple inheritance, the need to have a "virtual base class" that limits multiple inheritance is not needed, hence it does not apply to C#. Only members in C# can be virtual.

正如其他人提到的,由于 C# 没有多重继承,因此不需要限制多重继承的“虚拟基类”,因此它不适用于 C#。只有 C# 中的成员可以是虚拟的。