C# 中类成员的默认访问权限是什么?

csharpprogrammingserver side programming更新于 2025/10/13 14:52:17

C# 中类成员的默认访问权限是私有的。

成员变量,即类成员,是对象的属性(从设计角度来看),它们被设置为私有以实现封装。这些变量只能通过公共成员函数访问。

示例

using System;

namespace RectangleApplication {
   class Rectangle {
      //成员变量
      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();
      }
   }
}

相关文章