从子类C#中的基本抽象类覆盖属性的set部分

时间:2017-08-28 22:38:36

标签: c# inheritance properties override abstract-class

我有一个抽象类 A 作为我的基类,派生类 B 。如何覆盖子类 B set DrawingType 属性>

public abstract class A
{
    protected DrawingType m_type;

    public DrawingType Type
    {
        get { return m_type; }
        set { m_type = value}
    }
}

public B : A
{
    // I want to override the 'set' of DrawingType property here.
}

1 个答案:

答案 0 :(得分:0)

您的课程应如下所示:

abstract class A
{
    protected DrawingType m_type;

    protected virtual DrawingType Type
    {
        get { return m_type; }
        set { m_type = value; }
    }
}

class B : A
{
    protected override DrawingType Type
    {
        get
        {
            return m_type;
        }
        set
        {
            m_type = //custom logic goes here
        }
    }
}

如果基类没有实现,您可以使用

abstract class A
{
    protected DrawingType m_type;

    protected abstract DrawingType Type { get; set; }
}