unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); end;var Form1: TForm1;implementation{$R *.dfm}type TMyRec = record name: string; age: Word; end;procedure TForm1.Button1Click(Sender: TObject);var r1,r2: TMyRec; str: string;begin r1.name := '張三'; r1.age := 18; r2.name := '李四'; r2.age := 81; if r1.age > r2.age then str := r1.name else str := r2.name; ShowMessageFmt('%s年齡大', [str]); {李四年齡大}end;end.
type TMyRec = record name: string; age: Word; class operator GreaterThan(a,b: TMyRec): Boolean; end;
class operator TMyRec.GreaterThan(a,b: TMyRec): Boolean; {注意復(fù)制后再加上方法名: "TMyRec."}begin Result := a.age > b.age;end;
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); end;var Form1: TForm1;implementation{$R *.dfm}type TMyRec = record name: string; age: Word; class operator GreaterThan(a,b: TMyRec): Boolean; end;class operator TMyRec.GreaterThan(a,b: TMyRec): Boolean;begin Result := a.age > b.age;end;procedure TForm1.Button1Click(Sender: TObject);var r1,r2: TMyRec; str: string;begin r1.name := '張三'; r1.age := 18; r2.name := '李四'; r2.age := 81; if r1 > r2 then str := r1.name else str := r2.name; ShowMessageFmt('%s年齡大', [str]); {李四年齡大}end;end.
聯(lián)系客服