什么是 C# 中的方法隐藏?


方法隐藏也被称为阴影。父类的方法可以通过阴影而不使用 override 关键字在子类中获得。子类有其自己的相同函数版本。

使用 new 关键字执行阴影。

我们看一个例子。

示例

 在线演示

using System;
using System.Collections.Generic;

class Demo {
   public class Parent {
      public string GetInfo () {
         return "This is Parent Class!";
      }
   }

   public class Child : Parent {
      public new string GetInfo() {
         return "This is Child Class!";
      }
   }

   static void Main(String[] args) {
      Child child = new Child();
      Console.WriteLine(child. GetInfo());
   }
}

输出

This is Child Class!

更新于:22-06-2020

1K+ 浏览

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告
© . All rights reserved.