在 C# 中類受保護內部成員變數的範圍是什麼?


受保護的內部訪問說明符允許類向其他類物件和函式隱藏其成員變數和成員函式,但同一個應用程式中的子類除外。

在下面的示例中,派生類物件可以訪問受保護的內部變數。

示例

 即時演示

using System;
class One {
   protected internal int a = 50;
   private int b;
}
class Two : One {
   public Two() {
      Console.WriteLine(this.a);
   }
}
class Demo {
   static void Main() {
      Two t = new Two();
      // allowed since it is a derived class object
      t.a = 20;
   }
}

輸出

50

更新時間:2020 年 6 月 23 日

280 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.