Force base method call

后端 未结 13 1852
独厮守ぢ
独厮守ぢ 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: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

提交回复
热议问题