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


方法隐藏也称为影子。在影子中,子类可以使用父类的方法,而无需使用覆盖关键字。子类有自己版本的相同函数。

使用新关键字执行影子。

我们看一个例子。

示例

 在线演示

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!

更新于: 2020 年 6 月 22 日

1K+ 浏览次数

开启您的 职业生涯

完成课程,获得认证

开始
广告
© . All rights reserved.