什麼是 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.