“遗产”覆盖函数

发布于 2024-08-14 08:05:35 字数 688 浏览 3 评论 0原文

在某种程度上,这更多的是一个思考练习,而不是一个真正的问题,因为我没有足够的 CustomFS 类来特别担心仅使用复制粘贴。但我想知道是否有更好的方法。

假设我有几个类 CustomFSCustomFS2 等,它们都继承自 FSFS2 等FS/FS2等。全部继承自 FSG,它具有函数 GetStuff。假设我没有能力修改 FS 和 FSG,我如何仅使用一个 CustomFS 类来覆盖许多 FS/FS2 中的特定函数,并且无需使用 FS 构造 CustomFS 并为所有 FS 方法添加包装函数自定义FS。

当前策略:复制/粘贴:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空气里的味道 2024-08-21 08:05:35

除非通过代理或通过 Reflection.Emit 发出您自己的派生类,否则您不能这样做。

但是,如果您需要在每个类上执行更复杂的功能,您可能需要创建一个辅助方法(可能是通用的和静态的)来完成实际工作。

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.

森末i 2024-08-21 08:05:35

如果我正确理解你的问题,这似乎很适合策略设计模式: http ://www.dofactory.com/patterns/patternstrategy.aspx

如果您的覆盖函数比几行更复杂,这可能才有意义。但是,基本上,您可以让一个类 StuffGetter 拥有自己的方法 GetStuff

public class StuffGetter
{
    public int GetStuff(int rawStuff)
    {
        return rawStuff + 1 // presumably, the real example is more complicated than 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);
    }
}

基本上,您传入一个 StuffGetter 实例到任何需要自定义 GetStuff 实现的类。作为替代方案,您可以将 StuffGetter 设为静态类(这将使您无需传入实例),但这不太灵活。例如,如果您需要根据情况使用两个不同的 GetStuff 实现,使用实例,您只需传入(并存储)包含所需实现的实例即可。

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.

暖风昔人 2024-08-21 08:05:35
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;
    }
}
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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文