詳解C#中的接口屬性以及屬性訪問器的訪問限制
接口屬性
可以在接口上聲明屬性。以下是接口索引器訪問器的示例:
public interface ISampleInterface
{
// Property declaration:
string Name
{
get;
set;
}
}
接口屬性的訪問器不具有體。因此,訪問器的用途是指示屬性是否為讀寫、只讀或只寫。
在此例中,接口 IEmployee 具有讀寫屬性 Name 和只讀屬性 Counter。 Employee 類實現(xiàn) IEmployee 接口并使用這兩種屬性。程序讀取新雇員的姓名和雇員的當前編號,并顯示雇員姓名和計算所得的雇員編號。
可以使用屬性的完全限定名,它引用聲明成員的接口。例如:
string IEmployee.Name
{
get { return "Employee Name"; }
set { }
}
這稱為顯式接口實現(xiàn)(C# 編程指南)。例如,如果 Employee 類實現(xiàn)兩個接口 ICitizen 和 IEmployee,并且兩個接口都具有 Name 屬性,則需要顯式接口成員實現(xiàn)。即,如下屬性聲明:
string IEmployee.Name
{
get { return "Employee Name"; }
set { }
}
在 IEmployee 接口上實現(xiàn) Name 屬性,而下面的聲明:
string ICitizen.Name
{
get { return "Citizen Name"; }
set { }
}
在 ICitizen 接口上實現(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
限制訪問器可訪問性
屬性或索引器的 get 和 set 部分稱為“訪問器”。默認情況下,這些訪問器具有相同的可見性或訪問級別:其所屬屬性或索引器的可見性或訪問級別。不過,有時限制對其中某個訪問器的訪問會很有用。通常是在保持 get 訪問器可公開訪問的情況下,限制 set 訪問器的可訪問性。例如:
private string name = "Hello";
public string Name
{
get
{
return name;
}
protected set
{
name = value;
}
}
在此示例中,名為 Name 的屬性定義了一個 get 訪問器和一個 set 訪問器。 get 訪問器接受該屬性本身的可訪問性級別(在此示例中為 public),而對于 set 訪問器,則通過對該訪問器本身應(yīng)用 protected 訪問修飾符來進行顯式限制。
對訪問器的訪問修飾符的限制
對屬性或索引器使用訪問修飾符受以下條件的制約:
不能對接口或顯式接口成員實現(xiàn)使用訪問器修飾符。
僅當屬性或索引器同時具有 set 和 get 訪問器時,才能使用訪問器修飾符。這種情況下,只允許對其中一個訪問器使用修飾符。
如果屬性或索引器具有 override 修飾符,則訪問器修飾符必須與重寫的訪問器的訪問器(如果有的話)匹配。
訪問器的可訪問性級別必須比屬性或索引器本身的可訪問性級別具有更嚴格的限制。
重寫訪問器的訪問修飾符
在重寫屬性或索引器時,被重寫的訪問器對重寫代碼而言,必須是可訪問的。此外,屬性/索引器和訪問器的可訪問性級別都必須與相應(yīng)的被重寫屬性/索引器和訪問器匹配。例如:
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; }
}
}
實現(xiàn)接口
使用訪問器實現(xiàn)接口時,訪問器不能具有訪問修飾符。但是,如果使用一個訪問器(如 get)實現(xià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 { }
}
}
訪問器可訪問性域
如果對訪問器使用訪問某個修飾符,則訪問器的可訪問性域由該修飾符確定。
如果不對訪問器使用訪問修飾符,則訪問器的可訪問性域由屬性或索引器的可訪問性級別確定。
下面的示例包含三個類:BaseClass、DerivedClass 和 MainClass。每個類的 BaseClass、Name 和 Id 都有兩個屬性。該示例演示在使用限制性訪問修飾符(如 protected 或 private)時,如何通過 BaseClass 的 Id 屬性隱藏 DerivedClass 的 Id 屬性。因此,向該屬性賦值時,將調(diào)用 BaseClass 類中的屬性。將訪問修飾符替換為 public 將使該屬性可訪問。
該示例還演示 DerivedClass 的 Name 屬性的 set 訪問器上的限制性訪問修飾符(如 private 或 protected)如何防止對該訪問器的訪問,并在向它賦值時生成錯誤。
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
您可能感興趣的文章
- 01-10C#中查找Dictionary中的重復(fù)值的方法
- 01-10C#將圖片存放到SQL SERVER數(shù)據(jù)庫中的方法
- 01-10C#中Socket通信用法實例詳解
- 01-10關(guān)于nancy中的身份驗證
- 01-10C#中的事務(wù)用法實例分析
- 01-10C#裝箱和拆箱原理詳解
- 01-10C#接口實現(xiàn)方法實例分析
- 01-10C#類的多態(tài)性詳解
- 01-10C#創(chuàng)建不規(guī)則窗體的4種方式詳解
- 01-10C#實現(xiàn)讀取DataSet數(shù)據(jù)并顯示在ListView控件中的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery


