如何增加属性的访问修饰符 [英] How to increase the access modifier of a property

查看:63
本文介绍了如何增加属性的访问修饰符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一组类,在这些类中,一个共同的祖先负责设置各种属性所涉及的所有逻辑,而后代只是根据特定后代是否需要它们来更改对属性的访问. /p>

当我尝试如下所示进行操作时,出现编译器错误:覆盖受保护的"继承成员时,无法更改访问修饰符"

有没有一种方法可以实现我想做的事情?谢谢

public class Parent
{
     private int _propertyOne;
     private int _propertyTwo;

     protected virtual int PropertyOne
     {
          get { return _propertyOne; }
          set { _propertyOne = value; }
     }

     protected virtual int PropertyTwo
     {
          get { return _propertyTwo; }
          set { _propertyTwo = value; }
     }
}

public class ChildOne : Parent
{
    public override int PropertyOne  // Compiler Error CS0507
    {
        get { return base.PropertyOne; }
        set { base.PropertyOne = value; }
    }
    // PropertyTwo is not available to users of ChildOne
}

public class ChildTwo : Parent
{
    // PropertyOne is not available to users of ChildTwo
    public override int PropertyTwo  // Compiler Error CS0507
    {
        get { return base.PropertyTwo; }
        set { base.PropertyTwo = value; }
    }
}

解决方案

您可以使用"new"代替"override"来隐藏父级的受保护属性,如下所示:

public class ChildOne : Parent
{
    public new int PropertyOne  // No Compiler Error
    {
        get { return base.PropertyOne; }
        set { base.PropertyOne = value; }
    }
    // PropertyTwo is not available to users of ChildOne
}

public class ChildTwo : Parent
{
    // PropertyOne is not available to users of ChildTwo
    public new int PropertyTwo
    {
        get { return base.PropertyTwo; }
        set { base.PropertyTwo = value; }
    }
}

I'm trying to create a set of classes where a common ancestor is responsible for all the logic involved in setting various properties, and the descendants just change the access of properties depending on whether they are required in the particular descendant.

When I try to do it as shown below I get a compiler error: "cannot change access modifiers when overriding 'protected' inherited member"

Is there a way to achieve what I'm trying to do? Thanks

public class Parent
{
     private int _propertyOne;
     private int _propertyTwo;

     protected virtual int PropertyOne
     {
          get { return _propertyOne; }
          set { _propertyOne = value; }
     }

     protected virtual int PropertyTwo
     {
          get { return _propertyTwo; }
          set { _propertyTwo = value; }
     }
}

public class ChildOne : Parent
{
    public override int PropertyOne  // Compiler Error CS0507
    {
        get { return base.PropertyOne; }
        set { base.PropertyOne = value; }
    }
    // PropertyTwo is not available to users of ChildOne
}

public class ChildTwo : Parent
{
    // PropertyOne is not available to users of ChildTwo
    public override int PropertyTwo  // Compiler Error CS0507
    {
        get { return base.PropertyTwo; }
        set { base.PropertyTwo = value; }
    }
}

解决方案

You can do this by using "new" instead of "override" to hide the parent's protected property as follows:

public class ChildOne : Parent
{
    public new int PropertyOne  // No Compiler Error
    {
        get { return base.PropertyOne; }
        set { base.PropertyOne = value; }
    }
    // PropertyTwo is not available to users of ChildOne
}

public class ChildTwo : Parent
{
    // PropertyOne is not available to users of ChildTwo
    public new int PropertyTwo
    {
        get { return base.PropertyTwo; }
        set { base.PropertyTwo = value; }
    }
}

这篇关于如何增加属性的访问修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆