C# 中內部類變數的作用域是什麼?


使用 internal 訪問說明符設定內部變數。

internal double length;
internal double width;

可以在成員定義所在的應用程式中定義的任何類或方法中訪問具有 internal 訪問說明符的任何成員。

示例

 線上演示

using System;
namespace RectangleApplication {
   class Rectangle {
      //member variables
      internal double length;
      internal double width;
      double GetArea() {
         return length * width;
      }
      public void Display() {
         Console.WriteLine("Length: {0}", length);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
      }
   } //end class Rectangle
   class ExecuteRectangle {
      static void Main(string[] args) {
         Rectangle r = new Rectangle();
         r.length = 4.5;
         r.width = 3.5;
         r.Display();
         Console.ReadLine();
      }
   }
}

輸出

Length: 4.5
Width: 3.5
Area: 15.75

更新日期:2020-06-23

840 次瀏覽

啟動你的 職業

完成課程,獲得認證

開始
廣告
© . All rights reserved.