“遗产”覆盖函数
在某种程度上,这更多的是一个思考练习,而不是一个真正的问题,因为我没有足够的 CustomFS 类来特别担心仅使用复制粘贴。但我想知道是否有更好的方法。
假设我有几个类 CustomFS
、CustomFS2
等,它们都继承自 FS
、FS2
等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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除非通过代理或通过 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.
如果我正确理解你的问题,这似乎很适合策略设计模式: http ://www.dofactory.com/patterns/patternstrategy.aspx
如果您的覆盖函数比几行更复杂,这可能才有意义。但是,基本上,您可以让一个类
StuffGetter
拥有自己的方法GetStuff
:然后,您可以执行如下操作:
基本上,您传入一个
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 methodGetStuff
:Then, you would do something like this:
Basically, you pass in a
StuffGetter
instance to any class that needs your customGetStuff
implementation. As an alternative, you could makeStuffGetter
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 differentGetStuff
implementations depending on the situation, using instances, you could just pass in (and store) an instance containing the implementation you want.