Registriert seit: 8. Jan 2013
19 Beiträge
Delphi 10.3 Rio
|
AW: Member Class löschen
28. Jul 2014, 12:59
Wie wär's damit, den constructor auf die Instanz anzuwenden?
Sieht zwar ungewohnt aus, hat aber den Vorteil, die Vererbungshierarchie zu berücksichtigen.
Delphi-Quellcode:
type
TClearingClass = class
private
public
s: string;
i: integer;
constructor create;
end;
TClearingChild = class(TClearingClass)
public
d: double;
constructor create;
end;
implementation
{ TClearingClass }
constructor TClearingClass.create;
begin
inherited;
// explizit:
s := '';
i := 0;
end;
{ TClearingChild }
constructor TClearingChild.create;
begin
inherited;
d := 0.0;
end;
Beispiel:
Delphi-Quellcode:
type
TForm1 = class(TForm)
btnCreate: TSpeedButton;
btnInit: TSpeedButton;
procedure btnCreateClick(Sender: TObject);
procedure btnInitClick(Sender: TObject);
private
public
end;
implementation
procedure TForm1.btnCreateClick(Sender: TObject);
begin
ClearingChild := TClearingChild.create; //Constructor
showMessageFmt('s: %s, i: %d, d: %g',
[ClearingChild.s, ClearingChild.i, ClearingChild.d]); //s: , i: 0, d: 0
end;
procedure TForm1.btnInitClick(Sender: TObject);
begin
ClearingChild.s := 'changed';
ClearingChild.i := -1;
ClearingChild.d := -1.1;
ClearingChild.create; //Instanz initialisieren
showMessageFmt('s: %s, i: %d, d: %g',
[ClearingChild.s, ClearingChild.i, ClearingChild.d]); //s: , i: 0, d: 0
end;
"There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors."
Geändert von smallie (28. Jul 2014 um 13:18 Uhr)
|