Sign in Join
Wiki copytemporary

Inherited/ru

Deutsch · English · Français · Suomi · Русский

В переопределяемом виртуальном методе часто необходимо вызывать реализацию виртуального метода родительского class. Это можно сделать с помощью зарезервированного слова inherited. Аналогично, ключевое слово inherited может использоваться для вызова любого метода родительского class.

Вот простейший пример:

pascal
Type  
  TMyClass = Class(TComponent)  
    Constructor Create(AOwner : TComponent); override;  
  end; 

Constructor TMyClass.Create(AOwner : TComponent);  
begin  
  Inherited;  
  // Что-то делаем еще  
end;

Случаи конструкторов и деструкторов

Constructor, Пример 1 :

pascal
  ...
  TTest.Create;
  begin
    Inherited; // Ставится всегда в начале конструктора и запускает конструктор (только код) родительского класса
    ...
  end;

Constructor, Пример 2 :

pascal
  ...
  TTest.Create(...);
  begin
    Inherited Create(...); // Ставится всегда в начале конструктора и запускает конструктор (только код) родительского класса
    ...
  end;
  ...

Destructor, Пример 3 :

pascal
  TTest.Destroy;
  begin
    ...
    Inherited;  // Ставится всегда в конце деструктора и запускает деструктор (только код) родительского класса
  end;
  ...

Переопределение виртуальных методов

pascal
type  
  TMyClass = class(TStrings)  
    function GetObject(Index: Integer): TObject; override;  
  end; 

function TMyClass.GetObject(Index: Integer): TObject;
begin
  // Получаем результат из метода родительского класса 
  Result := inherited GetObject(Index);  
  // Делаем что-нибудь дальше
end;

From the Free Pascal wiki, page "Inherited/ru", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Edited on wiki.freepascal.org by 2 people, 2020–2022

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
ZoltanleoCreated page
Zoltanleo
Zoltanleo
Zoltanleovirtual methods override
Kai Burghardtresolve Category: Category:Pages using deprecated enclose attributes (minor)

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.