C# 程式設計/封裝
外觀
封裝是剝奪使用者對類中使用者不需要的資訊的訪問許可權,並阻止使用者以設計者未預期的方式操縱物件。
具有 公共保護級別 的類元素可供程式中任何位置的所有程式碼訪問。這些方法和屬性代表了允許外部使用者對類進行的操作。
具有 私有保護級別 的方法、資料成員(和其他元素)表示類的內部狀態(對於變數),以及不允許外部使用者執行的操作。所有類和結構成員的私有保護級別是預設的。這意味著,如果您沒有指定方法或變數的保護修飾符,編譯器會將其視為 私有。
例如
public class Frog
{
private int _height = 0;
// Methods
public void JumpLow() { Jump(1); }
public void JumpHigh() { Jump(10); }
void Jump(int height) { _height += height; }
}
在此示例中,Frog 類公開的公共方法是 JumpLow 和 JumpHigh。在內部,它們是使用私有 Jump 函式實現的,該函式可以跳到任何高度。此操作對外部使用者不可見,因此他們無法讓青蛙跳 100 米,只能跳 10 米或 1 米。私有方法 Jump 是透過更改私有資料成員 _height 的值來實現的,該成員也對外部使用者不可見。一些私有資料成員透過 屬性 可見。
私有成員只能在類本身內部訪問。另一個類中的方法,即使是從具有私有成員的類派生的類,也無法訪問這些成員。如果沒有指定保護級別,類成員將預設設定為私有。
namespace PrivateSample
{
public class Person
{
private string _name;
// Methods
public Person(string name)
{
// Private members can only be modified by the internal methods or constructors of class
this._name = name;
}
}
public class Entry
{
static void Main(string[] args)
{
Person OnePerson = new Person("Samanta");
//OnePerson._name = "Sam"; // This causes a error of access level
}
}
}
受保護的成員可以由類本身和從該類派生的任何類訪問。
namespace ProtectedSample
{
public class Person
{
protected string _name;
}
/// <summary>
/// When a class inherits from other class, it can access your protected and public members
/// above your created members
/// </summary>
public class Warrior : Person
{
public void SetName(string name)
{
// Protected members can be accessed by internal methods or constructors of class
// so, it can be accessed by inherit class too
base._name = name;
}
}
public class Entry
{
static void Main(string[] args)
{
Warrior OnePerson = new Warrior();
OnePerson.SetName("Glades"); // OK
// OnePerson._name = "Sam"; // This causes a error of access level too
// protected members can not be accessed by external scopes
}
}
}
公共成員可以由任何類中的任何方法訪問。
namespace PublicSample
{
public class Person
{
public string Name;
}
public class Entry
{
static void Main(string[] args)
{
Person BeautifulPerson = new Person();
BeautifulPerson.Name = "Debora"; // OK, public member can be accessed by other scopes
}
}
}
良好的程式設計實踐是不將成員變數暴露給外部,除非必要。這對於只能透過 訪問器 和 修改器方法 (getter 和 setter)訪問的欄位尤其如此。常量成員變數例外。
內部成員只能在同一個程式集中訪問,在程式集外部不可見。如果頂級類的保護級別沒有指定,它們將被視為內部的,並且只能在程式集中訪問。
namespace InternalSample
{
public class Person
{
internal string Name;
}
public class Entry
{
static void Main(string[] args)
{
Person BeautifulPerson = new Person();
BeautifulPerson.Name = "Debora"; // OK, internal member can be accessed by other
// scopes in same assembly supposing that Person is in another assembly, by example a
// library, the name cannot be accessed. In another assembly source, this causes an error:
// BeautifulPerson.Name = "Debora"; // Cannot access internal member
}
}
}
受保護的內部成員可以從從該類派生的任何類或同一個程式集中的任何類訪問。因此,這意味著受保護的 或 內部。[1]
這裡,一個例子
namespace InternalSample
{
public class Person
{
protected internal string Name;
}
public class Entry
{
static void Main(string[] args)
{
Person BeautifulPerson = new Person();
BeautifulPerson.Name = "Debora"; // As above...
}
}
}
public class Book : InternalSample.Person
{
static void Main(string[] args)
{
InternalSample.Person BeautifulPerson = new InternalSample.Person();
string aName = BeautifulPerson.Name; // Can be accessed, as Book is derived from Person
}
}
- ↑ Joe Mayo (2007-04-27). "型別成員訪問修飾符". http://www.csharp-station.com/: C# STATION. Retrieved 2011-08-12.
從派生型別中的程式碼或同一個程式集中的程式碼。受保護的或內部的組合。
{{cite web}}: External link in(help)|location=