覆盖和调用基本属性集方法

时间:2014-08-11 06:14:58

标签: c# inheritance polymorphism override

是否可以覆盖属性,还可以调用基本属性集方法?

例如;在课程Child中,我想覆盖this[]运算符,还要调用基础this[]运算符?

public class Base {

    protected Branch properties = Branch.EmptyBranch; 

    public virtual Branch this[string attribKey] {
        get
        {
            return properties[attribKey]; // will return Branch.EmptyBranch if key doesn't exist
        }
        set
        {
            properties[attribKey] = value;
        }
    }
}

public class Child {

    protected uint dimensions = 3;

    public override Branch this[string attribKey] {
        // No need to override get as we dont have any custom functionality
        set
        {
            // Can I call the base 'set' method?
            base[attribKey];

            // Add custom functionality
            if (attribKey.Equals("data_2d"))
                dimensions = 2;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您可以像这样调用基本集方法,它不应该导致任何问题:

public override Branch this[string attribKey] {

        set
        {                
            base[attribKey] = value;    

            if (attribKey.Equals("data_2d"))
                dimensions = 2;
        }
    }