在 C# 中,類成員的預設訪問許可權是什麼?


在 C# 中,類成員的預設訪問許可權是私有。

成員變數即類成員是物件(從設計角度來看)的屬性,並且保持私有以實現封裝。僅能使用公共成員函式訪問這些變數。

示例

using System;

namespace RectangleApplication {
   class Rectangle {
      //member variables
      private double length;
      private double width;

      public void Acceptdetails() {
         length = 10;
         width = 14;
      }

      public 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.Acceptdetails();
         r.Display();
         Console.ReadLine();
      }
   }
}

更新於: 2020 年 6 月 21 日

3K+ 檢視次數

開啟您的職業生涯

完成課程獲得證書認證

開始學習
廣告
© . All rights reserved.