詳解C#中的接口屬性和屬性拜訪器的拜訪限制。本站提示廣大學習愛好者:(詳解C#中的接口屬性和屬性拜訪器的拜訪限制)文章只能為提供參考,不一定能成為您想要的結果。以下是詳解C#中的接口屬性和屬性拜訪器的拜訪限制正文
接口屬性
可以在接口上聲明屬性。以下是接口索引器拜訪器的示例:
public interface ISampleInterface
{
// Property declaration:
string Name
{
get;
set;
}
}
接口屬性的拜訪器不具有體。是以,拜訪器的用處是指導屬性能否為讀寫、只讀或只寫。
在此例中,接口 IEmployee 具有讀寫屬性 Name 和只讀屬性 Counter。 Employee 類完成 IEmployee 接口並應用這兩種屬性。法式讀取新雇員的姓名和雇員確當前編號,並顯示雇員姓名和盤算所得的雇員編號。
可使用屬性的完整限制名,它援用聲明成員的接口。例如:
string IEmployee.Name
{
get { return "Employee Name"; }
set { }
}
這稱為顯式接話柄現(C# 編程指南)。例如,假如 Employee 類完成兩個接口 ICitizen 和 IEmployee,而且兩個接口都具有 Name 屬性,則須要顯式接口成員完成。即,以下屬性聲明:
string IEmployee.Name
{
get { return "Employee Name"; }
set { }
}
在 IEmployee 接口上完成 Name 屬性,而上面的聲明:
string ICitizen.Name
{
get { return "Citizen Name"; }
set { }
}
在 ICitizen 接口上完成 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 拜訪器,則經由過程對該拜訪器自己運用 protected 拜訪潤飾符來停止顯式限制。
對拜訪器的拜訪潤飾符的限制
對屬性或索引器應用拜訪潤飾符受以下前提的制約:
不克不及對接口或顯式接口成員完成應用拜訪器潤飾符。
僅當屬性或索引器同時具有 set 和 get 拜訪器時,能力應用拜訪器潤飾符。這類情形下,只許可對個中一個拜訪器應用潤飾符。
假如屬性或索引用具有 override 潤飾符,則拜訪器潤飾符必需與重寫的拜訪器的拜訪器(假如有的話)婚配。
拜訪器的可拜訪性級別必需比屬性或索引器自己的可拜訪性級別具有更嚴厲的限制。
重寫拜訪器的拜訪潤飾符
在重寫屬性或索引器時,被重寫的拜訪器對重寫代碼而言,必需是可拜訪的。另外,屬性/索引器和拜訪器的可拜訪性級別都必需與響應的被重寫屬性/索引器和拜訪器婚配。例如:
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; }
}
}
完成接口
應用拜訪器完成接口時,拜訪器不克不及具有拜訪潤飾符。然則,假如應用一個拜訪器(如 get)完成接口,則另外一個拜訪器可以具有拜訪潤飾符,以下面的示例所示:
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 屬性。是以,向該屬性賦值時,將挪用 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