詳解C#中的接口屬性以及屬性訪問(wèn)器的訪問(wèn)限制
接口屬性
可以在接口上聲明屬性。以下是接口索引器訪問(wèn)器的示例:
public interface ISampleInterface
{
// Property declaration:
string Name
{
get;
set;
}
}
接口屬性的訪問(wèn)器不具有體。因此,訪問(wèn)器的用途是指示屬性是否為讀寫(xiě)、只讀或只寫(xiě)。
在此例中,接口 IEmployee 具有讀寫(xiě)屬性 Name 和只讀屬性 Counter。 Employee 類實(shí)現(xiàn) IEmployee 接口并使用這兩種屬性。程序讀取新雇員的姓名和雇員的當(dāng)前編號(hào),并顯示雇員姓名和計(jì)算所得的雇員編號(hào)。
可以使用屬性的完全限定名,它引用聲明成員的接口。例如:
string IEmployee.Name
{
get { return "Employee Name"; }
set { }
}
這稱為顯式接口實(shí)現(xiàn)(C# 編程指南)。例如,如果 Employee 類實(shí)現(xiàn)兩個(gè)接口 ICitizen 和 IEmployee,并且兩個(gè)接口都具有 Name 屬性,則需要顯式接口成員實(shí)現(xiàn)。即,如下屬性聲明:
string IEmployee.Name
{
get { return "Employee Name"; }
set { }
}
在 IEmployee 接口上實(shí)現(xiàn) Name 屬性,而下面的聲明:
string ICitizen.Name
{
get { return "Citizen Name"; }
set { }
}
在 ICitizen 接口上實(shí)現(xiàn) Name 屬性。
interface IEmployee
{
string Name
{
get;
set;
}
int Counter
{
get;
}
}
public class Employee : IEmployee
{
public static int numberOfEmployees;
private string name;
public string Name // read-write instance property
{
get
{
return name;
}
set
{
name = value;
}
}
private int counter;
public int Counter // read-only instance property
{
get
{
return counter;
}
}
public Employee() // constructor
{
counter = ++counter + numberOfEmployees;
}
}
class TestEmployee
{
static void Main()
{
System.Console.Write("Enter number of employees: ");
Employee.numberOfEmployees = int.Parse(System.Console.ReadLine());
Employee e1 = new Employee();
System.Console.Write("Enter the name of the new employee: ");
e1.Name = System.Console.ReadLine();
System.Console.WriteLine("The employee information:");
System.Console.WriteLine("Employee number: {0}", e1.Counter);
System.Console.WriteLine("Employee name: {0}", e1.Name);
}
}
比如這里我們輸入:
210 Hazem Abolrous
則示例輸出
Enter number of employees: 210 Enter the name of the new employee: Hazem Abolrous The employee information: Employee number: 211 Employee name: Hazem Abolrous
限制訪問(wèn)器可訪問(wèn)性
屬性或索引器的 get 和 set 部分稱為“訪問(wèn)器”。默認(rèn)情況下,這些訪問(wèn)器具有相同的可見(jiàn)性或訪問(wèn)級(jí)別:其所屬屬性或索引器的可見(jiàn)性或訪問(wèn)級(jí)別。不過(guò),有時(shí)限制對(duì)其中某個(gè)訪問(wèn)器的訪問(wèn)會(huì)很有用。通常是在保持 get 訪問(wèn)器可公開(kāi)訪問(wèn)的情況下,限制 set 訪問(wèn)器的可訪問(wèn)性。例如:
private string name = "Hello";
public string Name
{
get
{
return name;
}
protected set
{
name = value;
}
}
在此示例中,名為 Name 的屬性定義了一個(gè) get 訪問(wèn)器和一個(gè) set 訪問(wèn)器。 get 訪問(wèn)器接受該屬性本身的可訪問(wèn)性級(jí)別(在此示例中為 public),而對(duì)于 set 訪問(wèn)器,則通過(guò)對(duì)該訪問(wèn)器本身應(yīng)用 protected 訪問(wèn)修飾符來(lái)進(jìn)行顯式限制。
對(duì)訪問(wèn)器的訪問(wèn)修飾符的限制
對(duì)屬性或索引器使用訪問(wèn)修飾符受以下條件的制約:
不能對(duì)接口或顯式接口成員實(shí)現(xiàn)使用訪問(wèn)器修飾符。
僅當(dāng)屬性或索引器同時(shí)具有 set 和 get 訪問(wèn)器時(shí),才能使用訪問(wèn)器修飾符。這種情況下,只允許對(duì)其中一個(gè)訪問(wèn)器使用修飾符。
如果屬性或索引器具有 override 修飾符,則訪問(wèn)器修飾符必須與重寫(xiě)的訪問(wèn)器的訪問(wèn)器(如果有的話)匹配。
訪問(wèn)器的可訪問(wèn)性級(jí)別必須比屬性或索引器本身的可訪問(wèn)性級(jí)別具有更嚴(yán)格的限制。
重寫(xiě)訪問(wèn)器的訪問(wèn)修飾符
在重寫(xiě)屬性或索引器時(shí),被重寫(xiě)的訪問(wèn)器對(duì)重寫(xiě)代碼而言,必須是可訪問(wèn)的。此外,屬性/索引器和訪問(wèn)器的可訪問(wèn)性級(jí)別都必須與相應(yīng)的被重寫(xiě)屬性/索引器和訪問(wèn)器匹配。例如:
public class Parent
{
public virtual int TestProperty
{
// Notice the accessor accessibility level.
protected set { }
// No access modifier is used here.
get { return 0; }
}
}
public class Kid : Parent
{
public override int TestProperty
{
// Use the same accessibility level as in the overridden accessor.
protected set { }
// Cannot use access modifier here.
get { return 0; }
}
}
實(shí)現(xiàn)接口
使用訪問(wèn)器實(shí)現(xiàn)接口時(shí),訪問(wèn)器不能具有訪問(wèn)修飾符。但是,如果使用一個(gè)訪問(wèn)器(如 get)實(shí)現(xiàn)接口,則另一個(gè)訪問(wèn)器可以具有訪問(wèn)修飾符,如下面的示例所示:
public interface ISomeInterface
{
int TestProperty
{
// No access modifier allowed here
// because this is an interface.
get;
}
}
public class TestClass : ISomeInterface
{
public int TestProperty
{
// Cannot use access modifier here because
// this is an interface implementation.
get { return 10; }
// Interface property does not have set accessor,
// so access modifier is allowed.
protected set { }
}
}
訪問(wèn)器可訪問(wèn)性域
如果對(duì)訪問(wèn)器使用訪問(wèn)某個(gè)修飾符,則訪問(wèn)器的可訪問(wèn)性域由該修飾符確定。
如果不對(duì)訪問(wèn)器使用訪問(wèn)修飾符,則訪問(wèn)器的可訪問(wèn)性域由屬性或索引器的可訪問(wèn)性級(jí)別確定。
下面的示例包含三個(gè)類:BaseClass、DerivedClass 和 MainClass。每個(gè)類的 BaseClass、Name 和 Id 都有兩個(gè)屬性。該示例演示在使用限制性訪問(wèn)修飾符(如 protected 或 private)時(shí),如何通過(guò) BaseClass 的 Id 屬性隱藏 DerivedClass 的 Id 屬性。因此,向該屬性賦值時(shí),將調(diào)用 BaseClass 類中的屬性。將訪問(wèn)修飾符替換為 public 將使該屬性可訪問(wèn)。
該示例還演示 DerivedClass 的 Name 屬性的 set 訪問(wèn)器上的限制性訪問(wèn)修飾符(如 private 或 protected)如何防止對(duì)該訪問(wèn)器的訪問(wèn),并在向它賦值時(shí)生成錯(cuò)誤。
public class BaseClass
{
private string name = "Name-BaseClass";
private string id = "ID-BaseClass";
public string Name
{
get { return name; }
set { }
}
public string Id
{
get { return id; }
set { }
}
}
public class DerivedClass : BaseClass
{
private string name = "Name-DerivedClass";
private string id = "ID-DerivedClass";
new public string Name
{
get
{
return name;
}
// Using "protected" would make the set accessor not accessible.
set
{
name = value;
}
}
// Using private on the following property hides it in the Main Class.
// Any assignment to the property will use Id in BaseClass.
new private string Id
{
get
{
return id;
}
set
{
id = value;
}
}
}
class MainClass
{
static void Main()
{
BaseClass b1 = new BaseClass();
DerivedClass d1 = new DerivedClass();
b1.Name = "Mary";
d1.Name = "John";
b1.Id = "Mary123";
d1.Id = "John123"; // The BaseClass.Id property is called.
System.Console.WriteLine("Base: {0}, {1}", b1.Name, b1.Id);
System.Console.WriteLine("Derived: {0}, {1}", d1.Name, d1.Id);
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
輸出:
Base: Name-BaseClass, ID-BaseClass Derived: John, ID-BaseClass
相關(guān)文章
c#的時(shí)間日期操作示例分享(c#獲取當(dāng)前日期)
這篇文章主要介紹了c#的時(shí)間日期操作示例,在給定時(shí)間戳返回指定的時(shí)間格式和獲取當(dāng)前時(shí)間方法,需要的朋友可以參考下2014-03-03
用.NET創(chuàng)建Windows服務(wù)的方法
用.NET創(chuàng)建Windows服務(wù)的方法...2007-03-03
unity實(shí)現(xiàn)動(dòng)態(tài)排行榜
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)動(dòng)態(tài)排行榜,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
C# Soap調(diào)用WebService的實(shí)例
下面小編就為大家?guī)?lái)一篇C# Soap調(diào)WebService的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
關(guān)于C#基礎(chǔ)知識(shí)回顧--反射(二)
其實(shí)說(shuō)白了,反射就是能知道我們未知類型的類型信息這么一個(gè)東西.沒(méi)什么神秘可講!反射的核心是System.Type。System.Type包含了很多屬性和方法,使用這些屬性和方法可以在運(yùn)行時(shí)得到類型信息2013-07-07
C#實(shí)現(xiàn)winform自動(dòng)關(guān)閉MessageBox對(duì)話框的方法
這篇文章主要介紹了C#實(shí)現(xiàn)winform自動(dòng)關(guān)閉MessageBox對(duì)話框的方法,實(shí)例分析了C#中MessageBox對(duì)話框的相關(guān)操作技巧,需要的朋友可以參考下2015-04-04

