c# - 什么是虚拟方法?

标签 c# virtual keyword

为什么要将方法声明为“虚拟”。

使用虚拟有什么好处?

最佳答案

Virtual修饰符用于标记可以在派生类中使用 override 修改方法\属性(等)修饰符。

例子:

class A
{
    public virtual void Foo()
       //DoStuff For A
}

class B : A
{
    public override void Foo()
    //DoStuff For B

    //now call the base to do the stuff for A and B 
    //if required
    base.Foo()
}

关于c# - 什么是虚拟方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/622132/

相关文章:

c# - 如何在 ASP.NET Core Web API 中的每个方法中进行模型验证?

c# - 选择数据的最佳方式是什么

jquery - 虚拟滚动/分页

Java super 和 this 关键字

seo - 谷歌有所有错误的关键字

c# - 通过 `Configuration.Install.Installer` 安装长期运行的服务时如何将参数传递给 `installutil`

c# - 如何获得屏幕的分辨率?对于 WinRT 应用程序?

javascript - 是否可以在服务器端制作浏览器?

linux - 在 Linux 上 : We see following: Physical, 真实内存、交换内存、虚拟内存 - 我们应该考虑哪个来调整大小?

c# - 在 C# 中使用 @ 的最佳实践