Difference between revisions of "Inherited"
From Lazarus wiki
Jump to navigationJump to searchLine 1: | Line 1: | ||
− | {{ | + | {{Inherited}} |
− | + | ||
− | In an overridden virtual method, it is often necessary to call the parent | + | |
+ | In an overridden virtual [[Method|method]], it is often necessary to call the parent [[Class|<syntaxhighlight lang="pascal" enclose="none">class</syntaxhighlight>]]’ implementation of the virtual method. | ||
+ | This can be done with the <syntaxhighlight lang="pascal" enclose="none">inherited</syntaxhighlight> [[Reserved word|reserved word]]. Likewise, the <syntaxhighlight lang="pascal" enclose="none">inherited</syntaxhighlight> [[Keyword|keyword]] can be used to call any method of the parent <syntaxhighlight lang="pascal" enclose="none">class</syntaxhighlight>. | ||
This case is the simplest: | This case is the simplest: | ||
Line 16: | Line 18: | ||
// Do more things | // Do more things | ||
end; | end; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == constructors and destructors case == | ||
+ | |||
+ | [[Constructor|<syntaxhighlight lang="pascal" enclose="none">Constructor</syntaxhighlight>]], example 1 : | ||
+ | <syntaxhighlight> | ||
+ | ... | ||
+ | TTest.Create; | ||
+ | begin | ||
+ | Inherited; // Always at the beginning of the constructors and start the constructor (code only) of the parent class | ||
+ | ... | ||
+ | end; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | <syntaxhighlight lang="pascal" enclose="none">Constructor</syntaxhighlight>, example 2 : | ||
+ | <syntaxhighlight> | ||
+ | ... | ||
+ | TTest.Create(...); | ||
+ | begin | ||
+ | Inherited Create(...); // Always at the beginning of the constructors and start the constructor (code only) of the parent class | ||
+ | ... | ||
+ | end; | ||
+ | ... | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | [[Destructor|<syntaxhighlight lang="pascal" enclose="none">Destructor</syntaxhighlight>]], example 3 : | ||
+ | <syntaxhighlight> | ||
+ | TTest.Destroy; | ||
+ | begin | ||
+ | ... | ||
+ | Inherited; // Always at the end of the destructors and start the destructor (code only) of the parent class | ||
+ | end; | ||
+ | ... | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 20:37, 29 March 2019
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
русский (ru) │
In an overridden virtual method, it is often necessary to call the parent class
’ implementation of the virtual method.
This can be done with the inherited
reserved word. Likewise, the inherited
keyword can be used to call any method of the parent class
.
This case is the simplest:
Type
TMyClass = Class(TComponent)
Constructor Create(AOwner : TComponent); override;
end;
Constructor TMyClass.Create(AOwner : TComponent);
begin
Inherited;
// Do more things
end;
constructors and destructors case
Constructor
, example 1 :
...
TTest.Create;
begin
Inherited; // Always at the beginning of the constructors and start the constructor (code only) of the parent class
...
end;
Constructor
, example 2 :
...
TTest.Create(...);
begin
Inherited Create(...); // Always at the beginning of the constructors and start the constructor (code only) of the parent class
...
end;
...
Destructor
, example 3 :
TTest.Destroy;
begin
...
Inherited; // Always at the end of the destructors and start the destructor (code only) of the parent class
end;
...