欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解C#中的接口属性以及属性访问器的访问限制

程序员文章站 2022-05-14 22:33:53
接口属性 可以在接口上声明属性。以下是接口索引器访问器的示例: public interface isampleinterface { // prope...

接口属性
可以在接口上声明属性。以下是接口索引器访问器的示例:

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