Difference between revisions of "Inherited"

From Free Pascal wiki
Jump to navigationJump to search
Line 16: Line 16:
 
   // Do more things   
 
   // Do more things   
 
end;  
 
end;  
</syntaxhighlight>
 
 
== Cas des constructeurs et destructeurs ==
 
Constructeur, Exemple 1 :<br>
 
<syntaxhighlight>
 
  ...
 
  TTest.Create;
 
  begin
 
    Inherited; // Toujours au début des constructeurs et démarre le constructeur (code seulement) de la classe parent
 
    ...
 
  end;
 
</syntaxhighlight>
 
 
Constructeur, exemple 2 :<br>
 
<syntaxhighlight>
 
  ...
 
  TTest.Create(...);
 
  begin
 
    Inherited Create(...); // Toujours au début des constructeurs et démarre le constructeur (code seulement) de la classe parent
 
    ...
 
  end;
 
  ...
 
</syntaxhighlight>
 
 
Destructeur, exemple 3 :<br>
 
<syntaxhighlight>
 
  TTest.Destroy;
 
  begin
 
    ...
 
    Inherited;  // Toujours à la fin des destructeurs et démarre le destructeur (code seulement) de la classe parent
 
  end;
 
  ...
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 23:16, 3 November 2016

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 keyword. 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;