Описание:
При освобождении объекта вызывается деструктор. Это позволяет объекту освободить память или другие временные ресурсы, которые он запросил.
Подписаться на рассылку:
Новые статьи:
|
DestructorЗадает метод, используемый для уничтожения объекта type Class declaration... Destructor Destroy; Override; ... end; Описание:
Ключевое слово Destructor задает процедуру-деструктор Destroy для класса.
При освобождении объекта вызывается деструктор. Это позволяет объекту освободить память или другие временные ресурсы, которые он запросил. Пример кода:
// Full Unit code.
// ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type // String holder record TString = string[10]; // Define a container class TWords = class private wordCount : Integer; wordsStart : Pointer; function Get(Index: Integer): string; public property GetWord[Index : Integer] : string read Get; published constructor Create(count : integer); Destructor Destroy; override; end; // The form class itself TForm1 = class(TForm) procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} // TWords constructor - build the word array constructor TWords.Create(count: integer); var i : Integer; wordsList : ^TString; begin // Get storage for 'count' strings GetMem(wordsStart, count*SizeOf(TString)); // Fill out this list wordsList := wordsStart; wordCount := count; for i := 1 to count do begin wordsList^ := 'Word '+IntToStr(i); Inc(wordsList); end; end; // TWords destructor - release storage destructor TWords.Destroy; begin // Release memory, if obtained if wordsStart <> nil then FreeMem(wordsStart); // Always call the parent destructor after running your own code inherited; end; // GetWord property read function function TWords.Get(Index: Integer): string; var wordsList : ^TString; begin // Read the word at the given index, if in range if (Index >= 1) and (Index <= wordcount) then begin wordsList := wordsStart; Inc(wordsList, Index-1); Result := wordsList^; end; end; // Main line code procedure TForm1.FormCreate(Sender: TObject); var words : TWords; begin // Create a TWords object words := TWords.Create(4); // Now show the 2nd word in this object ShowMessage('2nd word = '+words.GetWord[2]); end; end. Результат выполнения:
2nd word = Word 2
|
|
|