C#中的virtual和override关键字


风晓
风晓 2023-12-29 11:13:56 54003 赞同 0 反对 0
分类: 资源
本文主要讲了C#中的virtual和override关键字

使用基类中的引用

代码如下:

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

如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!

评价 0 条
风晓L1
粉丝 1 资源 2038 + 关注 私信
最近热门资源
银河麒麟桌面操作系统备份用户数据  127
统信桌面专业版【全盘安装UOS系统】介绍  122
银河麒麟桌面操作系统安装佳能打印机驱动方法  114
银河麒麟桌面操作系统 V10-SP1用户密码修改  105
最近下载排行榜
银河麒麟桌面操作系统备份用户数据 0
统信桌面专业版【全盘安装UOS系统】介绍 0
银河麒麟桌面操作系统安装佳能打印机驱动方法 0
银河麒麟桌面操作系统 V10-SP1用户密码修改 0
作者收入月榜
1

prtyaa 收益393.62元

2

zlj141319 收益218元

3

1843880570 收益214.2元

4

IT-feng 收益209.03元

5

风晓 收益208.24元

6

777 收益172.71元

7

Fhawking 收益106.6元

8

信创来了 收益105.84元

9

克里斯蒂亚诺诺 收益91.08元

10

技术-小陈 收益79.5元

请使用微信扫码

加入交流群

请使用微信扫一扫!