[英]C# method that can optionally be implemented by subclass, without a base method
I have Code OCD, so bear with me. 我有Code OCD,请多多包涵。
We have a base class with: 我们有一个基类:
virtual protected void OnHierarchyInitialized()
{
}
Now when someone overrides this method, they almost are to be expected to call the base method: 现在,当有人重写此方法时,几乎可以期望他们调用基本方法:
protected override void OnHierarchyInitialized()
{
base.OnHierarchyInitialized();
}
But.. nothing is happening in the base method. 但是..基本方法没有任何反应。 Nor will in the future.
将来也不会。 I just don't want people that subclass need to 'worry' when and if they need to call the base method.
我只是不希望子类的人在何时以及是否需要调用基本方法时 “担心”。 Plus: this is ugly fat in the implementing code, that distracts from the rest of the code that looks like a C# version of the Mona Lisa itself.
加:这是丑陋的脂肪在执行代码,从看起来像蒙娜丽莎本身的C#版本的代码,其余分散了。
Of course I could make it an abstract
method, but here is the big disadvantage that it needs to be implemented. 当然,我可以使其成为一种
abstract
方法,但这是需要实现的最大缺点。 Also screws up my work of art. 也搞砸了我的艺术作品。
Question 题
Is there something in between a virtual and abstract method, that tells the message: 在虚拟方法和抽象方法之间是否存在某些内容,可以告诉消息:
override me optionally, but there is no base method to call
? override me optionally, but there is no base method to call
?
Thanks in advance! 提前致谢!
Pretty sure the only thing you can do is use the built in XML documentation to notify the callers. 可以肯定,您唯一可以做的就是使用内置的XML文档来通知调用者。 For example, this is how it would show up to the person overriding your method:
例如,这是如何显示给覆盖您的方法的人的:
There is no keyword in c# to do that. 在c#中没有关键字可以做到这一点。
virtual
is the best there is for this approach. virtual
是这种方法的最佳选择。
Just don't worry about if there is an base class implementation or not. 只是不必担心是否有基类实现。 The one implementing the derived class shouldn't worry about it.
实现派生类的人不必为此担心。
There is just one question: Does the current implementation totally replace the base class's implementation? 只有一个问题:当前实现是否完全取代了基类的实现?
If it does, don't call the base class. 如果是这样,请不要调用基类。 If it doesn't: call it.
如果没有,请调用它。
If your code looks like this: 如果您的代码如下所示:
public void Foo()
{
// do something
OnHierarchyInitialized();
// do something else
}
You may want to consider events instead of inheritance: 您可能需要考虑事件而不是继承:
public event EventHandler HierarchyInitialized = delegate { };
OnHierarchyInitialized();
public void Foo()
{
// do something
OnHierarchyInitialized(new EventArgs());
// do something else
}
private void OnHierarchyInitialized(EventArgs e)
{
EventHandler handler = HierarchyInitialized;
handler(this, e);
}
Where consumers can then subscribe to the HierarchyInitialized
event when they're interested in it. 消费者对感兴趣的事件然后可以在其中订阅
HierarchyInitialized
事件。
If you're absolutely sure that the base method will never actually do anything, then an abstract
method is exactly the right approach. 如果您绝对确定基本方法永远不会做任何事情,那么
abstract
方法就是正确的方法。 I know you say this will screw up your work of art, but think about it like this: you want a way to explicitly tell the user that the base method does nothing. 我知道您说过这会破坏您的艺术作品,但您应该这样想:您想要一种明确地告诉用户基本方法无能为力的方法。 An
abstract
method will accomplish this, whilst reminding them that the method is there, even if they choose not to use it. 一个
abstract
方法将完成此任务,同时提醒他们即使没有选择使用该方法,该方法也已存在。
Think about how interfaces work - they are a collection of abstract methods that do nothing but have to be overridden. 考虑一下接口是如何工作的-它们是抽象方法的集合,它们什么都不做,但必须被覆盖。 This is the correct approach, and compliments your work of art by enforcing the inclusion of the method.
这是正确的方法,并通过强制包含该方法来补充您的艺术作品。 The empty method shows the user that they can hook into the
OnHierarchyInitialized
method to achieve something. 空方法向用户显示,他们可以加入
OnHierarchyInitialized
方法中以实现目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.