其他分享
首页 > 其他分享> > Delphi中record 的运算符重载

Delphi中record 的运算符重载

作者:互联网

-----------------------------------

Delphi中目前好像只能在Record中定义运算法重载的方法,C++的运算符重载定义在类中,有区别,以后有啥研究再记录下来,方便学习交流

 

---------------------------------------

 

资料来源:delphi 中record 的类操作符重载简介 - findumars - 博客园  https://www.cnblogs.com/findumars/p/10201381.html

Operator Overloading (Delphi) - RAD Studio  http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Operator_Overloading_(Delphi)

 

 

TXalionRec=record
ival:integer;
dval:Tdatetime;
constructor create(s:string);
{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;

 

{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(s:string);
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 TForm1.Button3Click(Sender: TObject);
var
myrec,rec2:TXalionRec;
d:Tdatetime;
begin
myrec:=3; //等于整数
memo1.Lines.Add('myrec ival='+ IntToStr(myrec.ival));
memo1.Lines.Add('myrec dval='+ formatdatetime('yyyy-mm-dd',myrec.dval));
memo1.Lines.Add('>>>>>>>>>>>>>>>>>>end<<<<<<<<<<<<<<<<');


inc(myrec); //递增
memo1.Lines.Add('myrec ival='+ IntToStr(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='+ IntToStr(myrec.ival));
memo1.Lines.Add('myrec dval='+ formatdatetime('yyyy-mm-dd',myrec.dval));
memo1.Lines.Add('>>>>>>>>>>>>>>>>>>end<<<<<<<<<<<<<<<<');


myrec:=myrec+5; //加整数

memo1.Lines.Add('myrec ival='+ IntToStr(myrec.ival));
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='+ IntToStr(myrec.ival));
memo1.Lines.Add('myrec dval='+ formatdatetime('yyyy-mm-dd',myrec.dval));
memo1.Lines.Add('>>>>>>>>>>>>>>>>>>end<<<<<<<<<<<<<<<<');


end;

 

标签:myrec,Delphi,class,运算符,record,result,operator,ival,TXalionRec
来源: https://www.cnblogs.com/dmqhjp/p/15029052.html