What is the meaning of the reintroduce and override directives in Delphi?

后端 未结 4 1605
余生分开走
余生分开走 2020-12-28 13:42

What is the difference between the override and reintroduce directives? And when should I not use the

4条回答
  •  梦谈多话
    2020-12-28 14:45

    And when should I not use the inherited keyword in overridden methods?

    Basically, the answer is when you don't wish the inherited method to execute. Be careful though as not allowing an inherited method to run may break functionality of an inherited object in a undesirable way, so make sure you are not introducing any unintended side effects.

    As an example, imagine you wish to fully override an inherited function called ApplyDiscount, but someone has hardcoded the discount percentage into the ancestor class. If you call the inherited ApplyDiscount it will override your code or calculate a value you will then override; in this case you could just not call inherited and apply the discount yourself.

    (This is a contrived example so if someone can think of a better one please add it.)

提交回复
热议问题