在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
今天简单介绍一下 delphi 中record 的类操作符重载使用,就是如何 实现 record 之间的简单操作。 关于类操作符重载 ,大家可以看 官方的文档。
Delphi allows certain functions, or "operators", to be overloaded within record declarations. The name of the operator function maps to a symbolic representation in source code. For example, the Add operator maps to the + symbol. The compiler generates a call to the appropriate overload, matching the context (that is, the return type, and type of parameters used in the call), to the signature of the operator function. The following table shows the Delphi operators that can be overloaded:
以下是通过实例来演示 TXalionRec=record ival:integer; dval:Tdatetime; constructor create; destructor Destroy; class operator Assign(var Dest:TXalionRec;const Src:TXalionRec); // 赋值 class operator NotEqual(ALeft,ARight:TXalionRec):boolean; // 不等于 class operator Equal(ALeft,ARight:TXalionRec):boolean; //等于 class operator GreaterThan(ALeft,ARight:TXalionRec):boolean; // 大于 class operator GreaterThanOrEqual(ALeft,ARight:TXalionRec):boolean; //大于等于 class operator LessThan(ALeft,ARight:TXalionRec):boolean; // 小于 class operator LessThanOrEqual(ALeft,ARight:TXalionRec):boolean; //小于等于 class operator Inc(AValue:TXalionRec):TXalionRec; // 递增 class operator Dec(AValue:TXalionRec):TXalionRec; // 递减 class operator Add(AValue1:TXalionRec; AValue2:integer):TXalionRec; // 加整数 class operator Add(AValue1:TXalionRec; AValue2:TDateTime):TXalionRec; //加时间 class operator Add(AValue1:TXalionRec; AValue2:TXalionRec):TXalionRec; // 直接加 class operator Implicit(AValue:TDateTime):TXalionRec; //显式等于日期 class operator Implicit(AValue:integer):TXalionRec; //显式等于整数 class operator Implicit(AValue:TXalionRec):TDateTime; //显式赋值日期 class operator Implicit(AValue:TXalionRec):integer; //显式赋值整数 end; var Form2: TForm2; implementation {$R *.dfm} { TXalionRec } class operator TXalionRec.Assign(var Dest:TXalionRec;const Src:TXalionRec); begin dest.ival:=src.ival; dest.dval:=src.dval; end; class operator TXalionRec.Add(AValue1: TXalionRec; AValue2: TDateTime): TXalionRec; begin result:= AValue1; result.dval:=result.dval+avalue2; end; class operator TXalionRec.Add(AValue1: TXalionRec; AValue2: integer): TXalionRec; begin result:= AValue1; result.ival:=result.ival+avalue2; end; class operator TXalionRec.Add(AValue1:TXalionRec; AValue2:TXalionRec):TXalionRec; begin result.ival :=avalue1.ival+avalue2.ival; result.dval:= avalue1.dval+avalue2.dval; end; constructor TXalionRec.create; begin ival:=0; dval:=now; end; class operator TXalionRec.Dec(AValue: TXalionRec): TXalionRec; begin result:=Avalue; dec(result.ival); end; destructor TXalionRec.Destroy; begin exit; end; class operator TXalionRec.Equal(ALeft, ARight: TXalionRec): boolean; begin result:=False; if Aleft.ival=Aright.ival then begin result:=True; end; end; class operator TXalionRec.GreaterThan(ALeft, ARight: TXalionRec): boolean; begin result:=False; if Aleft.ival>Aright.ival then result:=True; end; class operator TXalionRec.GreaterThanOrEqual(ALeft, ARight: TXalionRec): boolean; begin result:=False; if Aleft.ival>=Aright.ival then result:=True; end; class operator TXalionRec.Implicit(AValue: integer): TXalionRec; begin result.ival:=Avalue; end; class operator TXalionRec.Implicit(AValue: TDateTime): TXalionRec; begin result.dval:=Avalue; end; class operator TXalionRec.Implicit(AValue: TXalionRec): integer; begin result:=Avalue.ival; end; class operator TXalionRec.Implicit(AValue: TXalionRec): TDateTime; begin result:=Avalue.dval; end; class operator TXalionRec.Inc(AValue: TXalionRec): TXalionRec; begin result:=Avalue; inc( result.ival); end; class operator TXalionRec.LessThan(ALeft, ARight: TXalionRec): boolean; begin result:=False; if Aleft.ival<Aright.ival then result:=True; end; class operator TXalionRec.LessThanOrEqual(ALeft, ARight: TXalionRec): boolean; begin result:=False; if Aleft.ival<=Aright.ival then result:=True; end; class operator TXalionRec.NotEqual(ALeft, ARight: TXalionRec): boolean; begin result:=False; if Aleft.ival<>Aright.ival then result:=True; end; procedure TForm2.Button1Click(Sender: TObject); var myrec,rec2:TXalionRec; d:Tdatetime; begin myrec:=3; //等于整数 memo1.Lines.Add('myrec ival='+ myrec.ival.ToString); memo1.Lines.Add('myrec dval='+ formatdatetime('yyyy-mm-dd',myrec.dval)); memo1.Lines.Add('>>>>>>>>>>>>>>>>>>end<<<<<<<<<<<<<<<<'); inc(myrec); //递增 memo1.Lines.Add('myrec ival='+ myrec.ival.ToString); memo1.Lines.Add('myrec dval='+ formatdatetime('yyyy-mm-dd',myrec.dval)); memo1.Lines.Add('>>>>>>>>>>>>>>>>>>end<<<<<<<<<<<<<<<<'); d:=2; myrec:=myrec+ d; //加时间 2天 memo1.Lines.Add('myrec ival='+ myrec.ival.ToString); memo1.Lines.Add('myrec dval='+ formatdatetime('yyyy-mm-dd',myrec.dval)); memo1.Lines.Add('>>>>>>>>>>>>>>>>>>end<<<<<<<<<<<<<<<<'); myrec:=myrec+5; //加整数 memo1.Lines.Add('myrec ival='+ myrec.ival.ToString); memo1.Lines.Add('myrec dval='+ formatdatetime('yyyy-mm-dd',myrec.dval)); memo1.Lines.Add('>>>>>>>>>>>>>>>>>>end<<<<<<<<<<<<<<<<'); rec2:=6; myrec:=myrec+rec2; memo1.Lines.Add('myrec ival='+ myrec.ival.ToString); memo1.Lines.Add('myrec dval='+ formatdatetime('yyyy-mm-dd',myrec.dval)); memo1.Lines.Add('>>>>>>>>>>>>>>>>>>end<<<<<<<<<<<<<<<<'); end;
运行结果如图 可以看见非常灵活的实现各种操作,非常方便。
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论