简体   繁体   English

C#方法,可以选择通过子类实现,而无需基本方法

[英]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: 例如,这是如何显示给覆盖您的方法的人的:

XML文档

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.

相关问题 C# - 通过子类实现的接口继承和使用时,找不到方法的用法 - C# - Can't find usage of method when inherited and used through an interface implemented by the subclass 通过由基类c#实现的接口调用类的方法 - Calling a method of a class through the interface implemented by the base class c# C# 子类,方法相同 - C# Subclass with same method 限制泛型子类方法在c#中可以接受的类型 - Restrict types a generic subclass method can accept in c# 在Base类中声明的C#接口方法不需要在Derived类中再次实现 - C# interface method declared in Base class need not be again implemented in Derived class C#解决基类以纠正基于实现类型的存储库方法调用 - C# Resolving Base Class to Correct Repository Method Call Based on Implemented Type 防止在派生类C#中调用基类实现的接口方法 - Prevent calling base class implemented interface method in the derived class C# C#抱怨没有实现接口方法 - C# Complains that an Interface Method is not Implemented 在C#基类中基于动态参数调用查找子类中的方法 - Find method in subclass based on dynamic parameter call in base class in C# C# 编译器如何区分抽象方法的具体子类 impl 和抽象基类中的重载? - How does the C# compiler distinguish between a concrete subclass impl of an abstract method and an overload in the abstract base?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM