简体   繁体   中英

Override And Call Base Property Set Method

Is it possible to override a property but also call the base property set method?

For example; in the class Child I want to override the this[] operator but also call the base this[] operator aswell?

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

You can call the base set method like this and it shouldn't cause any problems:

public override Branch this[string attribKey] {

        set
        {                
            base[attribKey] = value;    

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM