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.