unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
//simuliertes Unterobjekt
TDummyData =
class
private
FString:
string;
FInteger: integer;
public
property StringVal:
string read FString
write FString;
property IntVal: integer
read FInteger
write FInteger;
end;
//Klasse mit Kopierfunktion
TBaseDummy =
class(TPersistent)
private
FDummy: TDummyData;
public
constructor Create;
destructor Destroy;
override;
//Überschreiben von AssignTo
procedure AssignTo(Dest: TPersistent);
override;
property Dummy: TDummyData
read FDummy;
end;
TForm1 =
class(TForm)
Button1: TButton;
Button2: TButton;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private-Deklarationen }
FTest1, FTest2: TBaseDummy;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TBaseDummy }
procedure TBaseDummy.AssignTo(Dest: TPersistent);
begin
//wenn Dest ein TBaseDummy ist, Werte kopieren
if Assigned(Dest)
and (Dest
is TBaseDummy)
then
begin
TBaseDummy(Dest).Dummy.StringVal := FDummy.StringVal;
TBaseDummy(Dest).Dummy.IntVal := FDummy.IntVal;
end
else
inherited AssignTo(Dest);
end;
constructor TBaseDummy.Create;
begin
inherited;
FDummy := TDummyData.Create;
end;
destructor TBaseDummy.Destroy;
begin
FDummy.Free;
inherited;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//aktuelle Werte des 2. Objektes anzeigen
ShowMessage(Format('
%s: %d',[FTest2.Dummy.StringVal,FTest2.Dummy.IntVal]));
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//Werte kopieren
FTest2.Assign(FTest1);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FTest1 := TBaseDummy.Create;
FTest1.Dummy.StringVal := '
Erster String';
FTest1.Dummy.IntVal := 1;
FTest2 := TBaseDummy.Create;
FTest2.Dummy.StringVal := '
Zweiter String';
FTest2.Dummy.IntVal := 2;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FTest1.Free;
FTest2.Free;
end;
end.