博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
对访问器的访问修饰符的限制
阅读量:7091 次
发布时间:2019-06-28

本文共 2266 字,大约阅读时间需要 7 分钟。

对属性或索引器使用访问修饰符受以下条件的制约:

·                 不能对接口或显式接口成员实现使用访问器修饰符。

·                 仅当属性或索引器同时具有 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 { }

    }

}

访问器可访问性域

如果对访问器使用访问某个修饰符,则访问器的可访问性域由该修饰符确定。

如果不对访问器使用访问修饰符,则访问器的可访问性域由属性或索引器的可访问性级别确定。

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);

    }

}

转载于:https://www.cnblogs.com/wwwzzg168/p/3572002.html

你可能感兴趣的文章
oracle字符集与客户端
查看>>
java线:辛格尔顿隐藏ThreadLocal实现线程数据共享
查看>>
MassTransit RabbitMQ 参考文档
查看>>
android 49 广播接收者中启动其他组件
查看>>
MySQL索引原理及慢查询优化
查看>>
JNI开发示例
查看>>
从netty-example分析Netty组件
查看>>
[经验]无线鼠标和无线键盘真的不能用了?——雷柏的重生之路~
查看>>
HTTP协议
查看>>
django的hello world 项目
查看>>
canvas线条的属性
查看>>
基础006_pg109_IP-Xfft
查看>>
[二] java8 函数式接口详解 函数接口详解 lambda表达式 匿名函数 方法引用使用含义 函数式接口实例 如何定义函数式接口...
查看>>
为什么 c = tf.matmul(a, b) 不立即执行矩阵乘法?
查看>>
Linux 变量引用和命令替换
查看>>
各大互联网公司架构演进之路汇总
查看>>
[转载].NET平台测试驱动开发模拟框架Moq简明教程(简介)
查看>>
xmlHttp
查看>>
Eclipse编译报Javascript和jquery错的解决办法
查看>>
C#字符串的简单用法
查看>>