Suppose there is a base class. In it we declare an abstract virtual function. In the successor, we implement this function (overlap override). Question: Is it possible to call this function in the base class? PS: I ask because I have met with such a situation in someone else's code. Before that I thought it was impossible.
2 answers
Can. The point is that you will never have an instance of a base abstract class. There will be an instance of a specific child class in which this method is exactly overridden.
This design pattern, by the way, is called the Template method: in the base abstract class we collect some algorithm from the abstract methods. And the work of this algorithm will depend on the child specific classes.
Example: TCocktail base class defines a cocktail preparation procedure. Child classes implement the specific steps of this process.
Base class:
type TCocktail = class protected procedure AddFirstIngridient(); virtual; abstract; //ΠΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΠΈΠ½Π³ΡΠΈΠ΄ΠΈΠ΅Π½ΡΠ° procedure AddSeconsIngridient(); virtual; abstract; //ΠΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ Π²ΡΠΎΡΠΎΠ³ΠΎ ΠΈΠ½Π³ΡΠΈΠ΄ΠΈΠ΅Π½ΡΠ° procedure AddDecorations(); virtual; abstract; //ΠΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΡΠΊΡΠ°ΡΠ΅Π½ΠΈΠΉ function GetName(): string; virtual; abstract; //ΠΠ°Π·Π²Π°Π½ΠΈΠ΅ public procedure Prepare; //ΠΠ»Π³ΠΎΡΠΈΡΠΌ ΠΏΡΠΈΠ³ΠΎΡΠΎΠ²Π»Π΅Π½ΠΈΡ ΠΊΠΎΠΊΡΠ΅ΠΉΠ»Ρ end; { TCocktail } procedure TCocktail.Prepare; begin writeln('ΠΠΎΡΠΎΠ²ΠΈΠΌ ΠΊΠΎΠΊΡΠ΅ΠΉΠ»Ρ ' + GetName()); AddFirstIngridient(); AddSeconsIngridient(); AddDecorations(); writeln('ΠΠ°Ρ ΠΊΠΎΠΊΡΠ΅ΠΉΠ»Ρ Π³ΠΎΡΠΎΠ²'); end; First cocktail
type TBloodyMary = class (TCocktail) protected procedure AddFirstIngridient();override; procedure AddSeconsIngridient();override; procedure AddDecorations();override; function GetName(): string;override; end; { TBloodyMary } procedure TBloodyMary.AddDecorations; begin writeln('ΠΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ Π»Π΅Π΄'); end; procedure TBloodyMary.AddFirstIngridient; begin writeln('50ΠΌΠ» Π²ΠΎΠ΄ΠΊΠΈ'); end; procedure TBloodyMary.AddSeconsIngridient; begin writeln('150ΠΌΠ» ΡΠΎΠΌΠ°ΡΠ½ΠΎΠ³ΠΎ ΡΠΎΠΊΠ°'); end; function TBloodyMary.GetName: string; begin result := 'ΠΡΠΎΠ²Π°Π²Π°Ρ ΠΡΡΠΈ'; end; Second cocktail
type TIrishCoffee = class (TCocktail) protected procedure AddFirstIngridient();override; procedure AddSeconsIngridient();override; procedure AddDecorations();override; function GetName(): string;override; end; { TBloodyMary } procedure TIrishCoffee.AddDecorations; begin writeln('50 ΡΠ»ΠΈΠ²ΠΊΠΈ (33%)'); end; procedure TIrishCoffee.AddFirstIngridient; begin writeln('50ΠΌΠ» ΠΈΡΠ»Π°Π½Π΄ΡΠΊΠΎΠ³ΠΎ Π²ΠΈΡΠΊΠΈ'); end; procedure TIrishCoffee.AddSeconsIngridient; begin writeln('80ΠΌΠ» ΠΊΠΎΡΠ΅'); end; function TIrishCoffee.GetName: string; begin result := 'ΠΡΠ»Π°Π½Π΄ΡΠΊΠΈΠΉ ΠΊΠΎΡΠ΅'; end; Well, the code itself:
var cocktail: TCocktail; begin cocktail := TBloodyMary.Create; cocktail.Prepare; cocktail := TIrishCoffee.Create; cocktail.Prepare; readln; end. - Is it possible to have a minimal example? - Sh4dow
- plus two) - Sh4dow
The call to the overlapped parent method is performed by the inherited service word.
- oneNot at the cashier. The question is not about calling an overlapped parent method from a child class, but about calling an implementing child from a parent. - Nofate β¦
- Oops, I'm sorry, I did not carefully read the question. - toxicdream