Einzelnen Beitrag anzeigen

Benutzerbild von Neutral General
Neutral General

Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#5

AW: [Aktuellere Delphis] Records = Klassen?

  Alt 31. Aug 2011, 16:05
Hallo,

Der Constructor bei Records ist eher eine Init-Methode, in der man Felder initialisieren kann.

Delphi-Quellcode:
type
  TPointEx = record
    X,Y: Integer;
    constructor Create(AX,AY: Integer);
  end;

constructor TPointEx.Create(AX,AY: Integer);
begin
  X := AX;
  Y:= AY;
end;

procedure TForm1.FormCreate(Sender: TObject);
var Pt: TPointEx;
begin
  Pt.X := 123;
  Pt.Y := 987;
  Caption := Format('X: %d, Y: %d',[Pt.X,Pt.Y]);

  Pt := TPointEx.Create(10,20);
  Caption := Caption + ' ' + Format('X: %d, Y: %d',[Pt.X,Pt.Y]);
end;
Beide Methoden funktionieren ohne Probleme.

Pt := TPointEx.Create(10,20);
ist quasi eine Alternative zu

Pt := Point(10,20);
Michael
"Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."
  Mit Zitat antworten Zitat