C Sharp/Encapsulation
外觀
< C Sharp
封裝(Encapsulation)是把類的方法、屬性、數據成員不讓用戶訪問或者限制其訪問。C#的類的成員默認是private訪問。
public class Frog
{
private int _height = 0;
// Methods
public void JumpLow() { Jump(1); }
public void JumpHigh() { Jump(10); }
void Jump(int height) { _height += height; }
}
保護級別
[編輯]Private
[編輯]Private成員只有類內的代碼可以訪問。這是缺省的保護級別。
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
}
}
}
Protected
[編輯]Protected的成員可以由派生類的代碼訪問。
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
}
}
}
Public
[編輯]Public的成員可以由任何代碼訪問。
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
}
}
}
Internal
[編輯]Internal保護的成員只能由同一個assembly的代碼看到並訪問。
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
}
}
}
Protected Internal
[編輯]Protected internal的成員可由派生類的代碼訪問,也可由同一個assembly的代碼訪問。即它是protected 或 internal.[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年4月27日).Type Member Access Modifiers.C# STATION.於2011年8月12日查閱.原文:「Either code from derived type or code in the same assembly. Combination of protected OR internal.」