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

csharpprogrammingserver side programming更新于 2025/10/13 8:22:17

方法隐藏也称为阴影。在阴影中,子类无需使用 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!

相关文章