Force base method call

后端 未结 13 1788
独厮守ぢ
独厮守ぢ 2020-12-11 00:15

Is there a construct in Java or C# that forces inheriting classes to call the base implementation? You can call super() or base() but is it possible to have it throw a comp

相关标签:
13条回答
  • 2020-12-11 00:49

    No. It is not possible. If you have to have a function that does some pre or post action do something like this:

    internal class Class1
    {
       internal virtual void SomeFunc()
       {
          // no guarantee this code will run
       }
    
    
       internal void MakeSureICanDoSomething()
       {
          // do pre stuff I have to do
    
          ThisCodeMayNotRun();
    
          // do post stuff I have to do
       }
    
       internal virtual void ThisCodeMayNotRun()
       {
          // this code may or may not run depending on 
          // the derived class
       }
    }
    
    0 讨论(0)
  • 2020-12-11 00:54

    Don't think there's any feasible solution built-in. I'm sure there's separate code analysis tools that can do that, though.

    0 讨论(0)
  • 2020-12-11 00:55

    I stumbled on to this post and didn't necessarily like any particular answer, so I figured I would provide my own ...

    There is no way in C# to enforce that the base method is called. Therefore coding as such is considered an anti-pattern since a follow-up developer may not realize they must call the base method else the class will be in an incomplete or bad state.

    However, I have found circumstances where this type of functionality is required and can be fulfilled accordingly. Usually the derived class needs a resource of the base class. In order to get the resource, which normally might be exposed via a property, it is instead exposed via a method. The derived class has no choice but to call the method to get the resource, therefore ensuring that the base class method is executed.

    The next logical question one might ask is why not put it in the constructor instead? The reason is that it may be an order of operations issue. At the time the class is constructed, there may be some inputs still missing.

    Does this get away from the question? Yes and no. Yes, it does force the derived class to call a particular base class method. No, it does not do this with the override keyword. Could this be helpful to an individual looking for an answer to this post, maybe.

    I'm not preaching this as gospel, and if individuals see a downside to this approach, I would love to hear about it.

    0 讨论(0)
  • 2020-12-11 00:56

    If I understand correctly you want to enforce that your base class behaviour is not overriden, but still be able to extend it, then I'd use the template method design pattern and in C# don't include the virtual keyword in the method definition.

    0 讨论(0)
  • 2020-12-11 00:58

    There isn't and shouldn't be anything to do that.

    The closest thing I can think of off hand if something like having this in the base class:

    public virtual void BeforeFoo(){}
    
    public void Foo()
    {
    
    this.BeforeFoo();
    //do some stuff
    this.AfterFoo();
    
    }
    
    public virtual void AfterFoo(){}
    

    And allow the inheriting class override BeforeFoo and/or AfterFoo

    0 讨论(0)
  • 2020-12-11 00:58

    You may want to look at this (call super antipatern) http://en.wikipedia.org/wiki/Call_super

    0 讨论(0)
提交回复
热议问题