C#中的virtual和override关键字

发布时间 2023-11-17 10:00:32作者: mingupupup

?公众号「DotNet学习交流」,分享学习DotNet的点滴。

使用基类中的引用

代码如下:

class MyBaseClass //基类
  {
       public void Print()
      {
           Console.WriteLine("This is the base class");
      }
       
  }

   class MyDerivedClass : MyBaseClass //派生类
  {
      new public void Print()
      {
           Console.WriteLine("This is the derived class");
      }
  }
   
   internal class Program
  {
       static void Main(string[] args)
      {
           MyDerivedClass myDerived = new MyDerivedClass();
           MyBaseClass myBaseClass = (MyBaseClass)myDerived; //转换成基类
                       myDerived.Print(); //从派生类部分调用Print方法
                       myBaseClass.Print(); //从基类部分调用Print方法
      }
  }

运行结果如下:

image-20231113203318239

可以发现派生类调用的是派生类的方法,而基类调用的是基类的方法。

虚方法和覆写方法

虚方法可以使基类的引用访问“升至”派生类内,可以使基类引用调用派生类(derived class)的方法,只需满足下面的条件。

1、派生类的方法和基类的方法有相同的签名和返回类型。

2、基类的方法使用virtual标注。

3、派生类的方法使用override标注。

使用virtual和override的例子如下:

class MyBaseClass //基类
  {
       virtual public void Print()
      {
           Console.WriteLine("This is the base class");
      }
       
  }

   class MyDerivedClass : MyBaseClass //派生类
  {
      override public void Print()
      {
           Console.WriteLine("This is the derived class");
      }
  }
   class SecondDerivedClass : MyDerivedClass //派生类
  {
       override public void Print()
      {
           Console.WriteLine("This is the second derived class");
      }
  }

   internal class Program
  {
       static void Main(string[] args)
      {
           SecondDerivedClass myDerived = new SecondDerivedClass();
           MyBaseClass myBaseClass = (MyBaseClass)myDerived; //转换成基类
                       myDerived.Print(); //从派生类部分调用Print方法
                       myBaseClass.Print(); //从基类部分调用Print方法
      }
  }

运行结果如下所示:

image-20231113210108171

不论是通过派生类调用还是通过基类调用,都会调用最高派生类中的方法,当通过基类调用时,调用被沿着继承层次向上传递,如下图所示:

image-20231113205459478

SecondDerivedClass中的Print方法声明为override,它会覆写方法中的全部两个低派生级别的版本。

如果将SecondDerivedClass中的Print方法声明为new,代码如下所示:

 class MyBaseClass //基类
  {
       virtual public void Print()
      {
           Console.WriteLine("This is the base class");
      }
       
  }

   class MyDerivedClass : MyBaseClass //派生类
  {
      override public void Print()
      {
           Console.WriteLine("This is the derived class");
      }
  }
   class SecondDerivedClass : MyDerivedClass //派生类
  {
       new public void Print()
      {
           Console.WriteLine("This is the second derived class");
      }
  }

   internal class Program
  {
       static void Main(string[] args)
      {
           SecondDerivedClass myDerived = new SecondDerivedClass();
           MyBaseClass myBaseClass = (MyBaseClass)myDerived; //转换成基类
                       myDerived.Print(); //从派生类部分调用Print方法
                       myBaseClass.Print(); //从基类部分调用Print方法
      }
  }

运行结果如下所示:

image-20231113210250760

当Print方法通过SecondDerivedClass的引用调用时,SecondDerivedClass中的方法被执行,而当方法通过MyBaseClass的引用调用时,方法调用只向上传递了一级,到达类MyDerivedClass,在那里被执行。该过程的图示如下所示:

image-20231113210605164

覆盖其他成员类型

其实属性事件以及索引器也是一样。

只读属性使用virtual/override的例子

代码如下:

 class MyBaseClass //基类
  {
       private int _myInt = 5;
       virtual public int MyProperty
      {
          get { return _myInt; }
      }
       
  }

   class MyDerivedClass : MyBaseClass //派生类
  {
       private int _myInt = 10;
       override public int MyProperty
      {
           get { return _myInt; }
      }
  }
 

   internal class Program
  {
       static void Main(string[] args)
      {
           MyDerivedClass myDerived = new MyDerivedClass();
           MyBaseClass myBaseClass = (MyBaseClass)myDerived; //转换成基类
           Console.WriteLine(myDerived.MyProperty);
           Console.WriteLine(myBaseClass.MyProperty);
      }
  }

运行结果如下所示:

image-20231114100512567