Difference between revisions of "Inherited"
From Free Pascal wiki
Jump to navigationJump to search (virtual method override example) |
|||
(One intermediate revision by one other user not shown) | |||
Line 2: | Line 2: | ||
− | In an overridden virtual [[Method|method]], it is often necessary to call the parent [[Class|<syntaxhighlight lang="pascal" | + | Back to [[Reserved words]]. |
− | This can be done with the <syntaxhighlight lang="pascal" | + | |
+ | |||
+ | In an overridden virtual [[Method|method]], it is often necessary to call the parent [[Class|<syntaxhighlight lang="pascal" inline>class</syntaxhighlight>]]’ implementation of the virtual method. | ||
+ | This can be done with the <syntaxhighlight lang="pascal" inline>inherited</syntaxhighlight> [[Reserved word|reserved word]]. Likewise, the <syntaxhighlight lang="pascal" inline>inherited</syntaxhighlight> [[Keyword|keyword]] can be used to call any method of the parent <syntaxhighlight lang="pascal" inline>class</syntaxhighlight>. | ||
This case is the simplest: | This case is the simplest: | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
Type | Type | ||
TMyClass = Class(TComponent) | TMyClass = Class(TComponent) | ||
Line 20: | Line 23: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | == Constructors and destructors case == |
− | [[Constructor|<syntaxhighlight lang="pascal" | + | [[Constructor|<syntaxhighlight lang="pascal" inline>Constructor</syntaxhighlight>]], example 1 : |
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
... | ... | ||
TTest.Create; | TTest.Create; | ||
Line 32: | Line 35: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | <syntaxhighlight lang="pascal" | + | <syntaxhighlight lang="pascal" inline>Constructor</syntaxhighlight>, example 2 : |
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
... | ... | ||
TTest.Create(...); | TTest.Create(...); | ||
Line 43: | Line 46: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | [[Destructor|<syntaxhighlight lang="pascal" | + | [[Destructor|<syntaxhighlight lang="pascal" inline>Destructor</syntaxhighlight>]], example 3 : |
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
TTest.Destroy; | TTest.Destroy; | ||
begin | begin | ||
Line 53: | Line 56: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | == Virtual methods override == |
<syntaxhighlight lang="pascal"> | <syntaxhighlight lang="pascal"> |
Latest revision as of 16:16, 6 August 2022
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
русский (ru) │
Back to Reserved words.
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;
...
Virtual methods override
type
TMyClass = class(TStrings)
function GetObject(Index: Integer): TObject; override;
end;
function TMyClass.GetObject(Index: Integer): TObject;
begin
// Get result from parent class method
Result := inherited GetObject(Index);
// Do something
end;