开发者

"heritance" to override a function

开发者 https://www.devze.com 2022-12-13 16:58 出处:网络
To some extent, this is more a thought exercise than a real problem, since I don\'t have enough CustomFS classes to be particularly bothered by just using copy paste.But I wonder if there\'s a better

To some extent, this is more a thought exercise than a real problem, since I don't have enough CustomFS classes to be particularly bothered by just using copy paste. But I wonder if there's a better way.

Suppose I have several classes CustomFS, CustomFS2, etc., all of which inherit from FS, FS2, etc. FS/FS2/etc. all inherit from FSG, which has a function GetStuff. Assuming I do not have the ability to modify FS and FSG how can I override a particular function in many of of the FS/FS2 w开发者_StackOverflow社区ith only one CustomFS class, and without constructing CustomFS with FS and adding a wrapper function for all of FS's methods to customFS.

Current strategy: Copy/paste:

class CustomFS : FS
{
    protected override int GetStuff(int x)
    {
        int retval = base.GetStuff(x);
        return retval + 1;
    }
}

class CustomFS2 : FS2
{
    protected override int GetStuff(int x)
    {
        int retval = base.GetStuff(x);
        return retval + 1;
    }
}


You can't, except by means of a proxy or by emitting your own derived classes via Reflection.Emit.

However, if you have more complex functionality which you need to perform on each class, you might want to create a helper method (probably generic and static) which does the actual work.


If I'm understanding your question correctly, this seems like a good fit for the strategy design pattern: http://www.dofactory.com/patterns/patternstrategy.aspx

This would probably only make sense if your override function is more complex than a couple lines. But, basically, you could have a class StuffGetter would have its own method GetStuff:

public class StuffGetter
{
    public int GetStuff(int rawStuff)
    {
        return rawStuff + 1 // presumably, the real example is more complicated than this
    }
}

Then, you would do something like this:

class CustomFS : FS
{
    private StuffGetter _stuffGetter { get; set; }

    public CustomFS(StuffGetter stuffGetter)
    {
        _stuffGetter = stuffGetter;
    }

    protected override int GetStuff(int x)
    {
        int retval = base.GetStuff(x);
        return _stuffGetter.GetStuff(retval);
    }
}

class CustomFS2 : FS2
{
    private StuffGetter _stuffGetter { get; set; }

    public CustomFS2(StuffGetter stuffGetter)
    {
        _stuffGetter = stuffGetter;
    }

    protected override int GetStuff(int x)
    {
        int retval = base.GetStuff(x);
        return _stuffGetter.GetStuff(retval);
    }
}

Basically, you pass in a StuffGetter instance to any class that needs your custom GetStuff implementation. As an alternative, you could make StuffGetter a static class (which would save you from needing to pass in an instance), but this is less flexible. For example, if you wanted two different GetStuff implementations depending on the situation, using instances, you could just pass in (and store) an instance containing the implementation you want.


class CustomFS : FS
{
    protected override int GetStuff(int x)
    {
        return CustomHelper.GetStuff(base.GetStuff(x));
    }
}

class CustomFS2 : FS2
{
    protected override int GetStuff(int x)
    {
        return CustomHelper.GetStuff(base.GetStuff(x));
    }
}

static class CustomHelper
{
    static int GetStuff(int x)
    {
        return x + 1;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号