zoukankan      html  css  js  c++  java
  • Make sure base method gets called in C#

    Make sure base method gets called in C#

    Can I somehow force a derived class to always call the overridden methods base?

    public class BaseClass
    {
        public virtual void Update()
        {
            if(condition)
            {
                throw new Exception("..."); // Prevent derived method to be called
            }
        }
    }
    

    And then in a derived class :

    public override void Update()
    {
        base.Update(); // Forced call
    
        // Do any work
    }
    

    I've searched and found a suggestion to use a non-virtual Update() but also a protected virtual UpdateEx(). It just doesn't feel very neat, isn't there any better way?

    I hope you get the question and I am sorry for any bad English.

    回答1

    Use the template method pattern - don't override the base method which needs to do some work, override one specific bit, which can either be abstract or a no-op in the base class. (The decision about whether to make it a no-op or abstract is usually fairly obvious - does the base class make sense on its own, as a concrete class?)

    It sounds like this is basically the pattern you've found with UpdateEx - although it's usually UpdateImpl or something similar in my experience. There's nothing wrong with this as a pattern, in my view - it avoids any idea of forcing all derived classes to write the same code to call the base method.

    回答2

    This took me a bit to get what Update and UpdateEx would look like. Here's a code example that might help others.

    public class BaseClass
    {
        // This is the Update that class instances will use, it's never overridden by a subclass
        public void Update()
        {
            if(condition);
            // etc... base class code that will always run
    
            UpdateEx(); // ensure subclass Update() functionality is run
        }
    
        protected virtual void UpdateEx()
        {
            // does nothing unless sub-class overrides
        }
    }
    

    A subclass will never have any implementation for Update(). It'll uses UpdateEx() to add implementation to Update();

    public class ConcreteClass : BaseClass
    {
        protected override void UpdateEx()
        {
            // implementation to be added to the BaseClass Update();
        }
    }
    

    Any instance of ConcreteClass will use the BaseClass implementation of Update() along with however ConcreteClass extends it with UpdateEx().

    我遇到的问题是多次继承,

    Base

    A:Base

    B:A

    A override method1 of Base

    B need add some extra work beyond method1

    所以,方法2更适合我,在A里面新加一个virtual方法,然后直接在A里面method1里面,新加的virtual方法

  • 相关阅读:
    【转载】PyQt QSetting保存设置
    Python WebDriver自动化测试
    Pyqt 控件的信号槽事件定义方法
    Pyqt SpVoice朗读功能
    Pyqt 国际化多语言支持
    MQTT研究之EMQ:【wireshark抓包分析】
    MQTT研究之EMQ:【SSL双向验证】
    ES6模板字符串【${}配合反单引号一起用】
    express中遇到的一个小问题“403”
    MQTT研究之EMQ:【基础研究】
  • 原文地址:https://www.cnblogs.com/chucklu/p/14760479.html
Copyright © 2011-2022 走看看