Restricting Accessor Accessibility (C# Programming Guide)-属性访问操作符可访问性的限制

来源:互联网 发布:私有属性 js 分享 编辑:程序博客网 时间:2024/05/16 01:17

下图是关于类属性各个部分名称:


MSDN原文:(http://msdn.microsoft.com/en-us/library/75e8y5dd.aspx)

The get andset portions of a property or indexer are calledaccessors. By default these accessors have the same visibility, or access level: that of the property or indexer to which they belong. For more information, seeaccessibility levels. However, it is sometimes useful to restrict access to one of these accessors. Typically, this involves restricting the accessibility of theset accessor, while keeping the get accessor publicly accessible. For example:

C#
private string name = "Hello";public string Name{    get    {        return name;    }    protected set    {        name = value;    }}

In this example, a property called Name defines a get and set accessor. Theget accessor receives the accessibility level of the property itself,public in this case, while the set accessor is explicitly restricted by applying theprotected access modifier to the accessor itself.


Restrictions on Access Modifiers on Accessors(属性访问操作符(如,get,set)访问标识(如,private,protected...)的限制)

Using the accessor modifiers on properties or indexers is subject to these conditions:

  • You cannot use accessor modifiers on an interface or an explicitinterface member implementation.

  • You can use accessor modifiers only if the property or indexer has bothset and get accessors. In this case, the modifier is permitted on one only of the two accessors.

  • If the property or indexer has anoverride modifier, the accessor modifier must match the accessor of the overridden accessor, if any.

  • The accessibility level on the accessor must be more restrictive than the accessibility level on the property or indexer itself.

属性或索引器使用标示符限制访问操作符时的几种情况:

  • 当是一个接口或者是一个接口成员的隐式实现时,不能使用标示符(private,protected...)限制访问访问操作符(get,set).

  • 只有属性或者索引器同时拥有set和get访问操作符时,才可以在操作符上使用标示符进行限定;并且在这种情况下,仅允许对一个属性访问操作符使用标识符。

  • 如果属性或者索引器有override标识符(也就是说这个属性或者索引器是重写的),那么重写的属性或者索引器的访问操作符的表示符一定要和被重写的属性或者索引器的匹配,无论是什么情况都要如此。

  • 在属性操作符上的可访问层次一定要比这个属性或者索引器本身的可访问层次要更严格。(也就是说你可以在protected属性中定义一个private的访问操作符,但不能定义一个public的访问操作符



Access Modifiers on Overriding Accessors

When you override a property or indexer, the overridden accessors must be accessible to the overriding code. Also, the accessibility level of both the property/indexer, and that of the accessors must match the corresponding overridden property/indexer and the accessors. For example:

C#
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; }    }}
Implementing Interfaces

When you use an accessor to implement an interface, the accessor may not have an access modifier. However, if you implement the interface using one accessor, such asget, the other accessor can have an access modifier, as in the following example:

C#
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 { }    }}

Accessor Accessibility Domain

If you use an access modifier on the accessor, theaccessibility domain of the accessor is determined by this modifier.

If you did not use an access modifier on the accessor, the accessibility domain of the accessor is determined by the accessibility level of the property or indexer.

访问操作符的可访问域:

(1)如果在访问操作符中定义了访问标识符,那么这个操作符的可访问域就是这个访问操作符的标示符表示的作用域。

(2)如果没有对访问操作符定义访问标识符,那么这个操作符的可访问与就是这个属性或者索引器的可访问层次。

如:

class Test

{

        public string Name

        {

                   get;

                   private set;

       }

}

上面这个类中的Name属性具有公共的读(get),私有的写(set,仅可以在类内部使用)



Example

The following example contains three classes, BaseClass,DerivedClass, and MainClass. There are two properties on theBaseClass, Name and Id on both classes. The example demonstrates how the property Id on DerivedClass can be hidden by the propertyId on BaseClass when you use a restrictive access modifier such asprotected or private. Therefore, when you assign values to this property, the property on theBaseClass class is called instead. Replacing the access modifier bypublic will make the property accessible.

The example also demonstrates that a restrictive access modifier, such as private or protected, on theset accessor of the Name property inDerivedClass prevents access to the accessor and generates an error when you assign to it.

C#
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();    }}/* Output:    Base: Name-BaseClass, ID-BaseClass    Derived: John, ID-BaseClass*/
Comments

Notice that if you replace the declaration new private string Id bynew public string Id, you get the output:

Name and ID in the base class: Name-BaseClass, ID-BaseClass

Name and ID in the derived class: John, John123



原创粉丝点击