C# 中類的預設訪問許可權是什麼?
如果沒有指定訪問修飾符,則預設值為 Internal。Internal 訪問說明符允許類向當前程式集中的其他函式和物件公開其成員變數和成員函式。換句話說,可以從定義成員所在應用程式中定義的任何類或方法訪問任何帶有內部訪問修飾符的成員。
以下是一個顯示如何使用 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
廣告