Diberdayakan oleh Blogger.

Konstruktor dan Destruktor (Delphi)




Konstruktor dan Destruktor (Delphi)
Dalam pembuatan objek atau kelas terdapat metode selain procedure dan function adalah Konstruktor dan
Destruktor. Konstruktor digunakan untuk menginisialisasi objek, dipanggil pada saat objek diciptakan. Sedangkan destruktor biasanya setelah objek tidak digunakan lagi. Pembuatan konstruktor dengan menggunakan key word “Constructor” sedangkan dekonstruktor menggunakan “Destructor”.
 
A. DESTRUKTOR
Bentuk Umum:
type Class declaration
    ...
    Destructor Destroy; Override;
    ...  
end;
Contoh Destruktor:
// 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
B. KONSTRUKTOR
Bentuk Umum:
1
  type Class declaration
    ...
    Constructor Name; {Overload;}
    ...
  end;
2
  type Class declaration
    ...
    Constructor Name(Arguments); {Overload;}
    ...
  end;
Contoh Konstruktor:
// 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
  Forms, Dialogs, Classes, Controls, StdCtrls;

type
  // Define a parent class, base on TObject by default
  TFruit = class
  public
    name   : string;
    Constructor Create; overload;   // This constructor uses defaults
    Constructor Create(name : string); overload;
  end;

  // Define a descendant types
  TApple = class(TFruit)
  public
    diameter : Integer;
  published
    Constructor Create(name : string; diameter : Integer);
  end;

  // The class for the form used by this unit
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation
{$R *.dfm} // Include form definitions

// Create a fruit object - parameterless version
constructor TFruit.Create;
begin
  // Execute the parent (TObject) constructor first
  inherited;  // Call the parent Create method

  // Now set a default fruit name
  self.name := 'Fruit';
end;

// Create a fruit object - parameterised version
constructor TFruit.Create(name: string);
begin
  // Cannot execute the parent constructor - parms differ

  // And save the fruit name
  self.name := name;
end;

// Create an apple object
constructor TApple.Create(name: string; diameter : Integer);
begin
  // Execute the parent (TFruit) constructor first
  inherited Create(name);  // Call the parent method

  // Now save the passed apple diameter
  self.diameter := diameter;
end;

// Main line code
procedure TForm1.FormCreate(Sender: TObject);
var
  fruit  : TFruit;
  banana : TFruit;
  apple  : TApple;

begin
  // Create 3 different fruit objects
  fruit  := TFruit.Create;
  banana := TFruit.Create('Banana');
  apple  := TApple.Create('Pink Lady', 12);

  // See which of our objects are fruits
  if fruit  Is TFruit then ShowMessage(fruit.name  +' is a fruit');
  if banana Is TFruit then ShowMessage(banana.name +' is a fruit');
  if apple  Is TFruit then ShowMessage(apple.name  +' is a fruit');

  // See which objects are apples
  if fruit  Is TApple then ShowMessage(fruit.name   +' is an apple');
  if banana Is TApple then ShowMessage(banana.name  +' is an apple');
  if apple  Is TApple then ShowMessage(apple.name   +' is an apple');
end;
end. 
   Fruit is a fruit
   Banana is a fruit
   Pink Lady is a fruit
   Pink Lady is an apple 
 
 
 
Sumber: http://www.delphibasics.co.uk/RTL.asp?Name=Destructor
              http://www.delphibasics.co.uk/RTL.asp?Name=Constructor
 

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 komentar:

Posting Komentar