1. 程式人生 > >delphi 中record 的類操作符過載簡介

delphi 中record 的類操作符過載簡介

delphi 10.3 裡面加強了對 record 的擴充套件,叫做  Managed Records。

今天簡單介紹一下 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:

Operator Category
Declaration Signature Symbol Mapping

Implicit

Conversion

Implicit(a : type) : resultType;

implicit typecast

Explicit

Conversion

Explicit(a: type) : resultType;

explicit typecast

Negative

Unary

Negative(a: type) : resultType;

-

Positive

Unary

Positive(a: type): resultType;

+

Inc

Unary

Inc(a: type) : resultType;

Inc

Dec

Unary

Dec(a: type): resultType

Dec

LogicalNot

Unary

LogicalNot(a: type): resultType;

not

Trunc

Unary

Trunc(a: type): resultType;

Trunc

Round

Unary

Round(a: type): resultType;

Round

In

Set

In(a: type; b: type) : Boolean;

in

Equal

Comparison

Equal(a: type; b: type) : Boolean;

=

NotEqual

Comparison

NotEqual(a: type; b: type): Boolean;

<>

GreaterThan

Comparison

GreaterThan(a: type; b: type) Boolean;

>

GreaterThanOrEqual

Comparison

GreaterThanOrEqual(a: type; b: type): Boolean;

>=

LessThan

Comparison

LessThan(a: type; b: type): Boolean;

<

LessThanOrEqual

Comparison

LessThanOrEqual(a: type; b: type): Boolean;

<=

Add

Binary

Add(a: type; b: type): resultType;

+

Subtract

Binary

Subtract(a: type; b: type) : resultType;

-

Multiply

Binary

Multiply(a: type; b: type) : resultType;

*

Divide

Binary

Divide(a: type; b: type) : resultType;

/

IntDivide

Binary

IntDivide(a: type; b: type): resultType;

div

Modulus

Binary

Modulus(a: type; b: type): resultType;

mod

LeftShift

Binary

LeftShift(a: type; b: type): resultType;

shl

RightShift

Binary

RightShift(a: type; b: type): resultType;

shr

LogicalAnd

Binary

LogicalAnd(a: type; b: type): resultType;

and

LogicalOr

Binary

LogicalOr(a: type; b: type): resultType;

or

LogicalXor

Binary

LogicalXor(a: type; b: type): resultType;

xor

BitwiseAnd

Binary

BitwiseAnd(a: type; b: type): resultType;

and

BitwiseOr

Binary

BitwiseOr(a: type; b: type): resultType;

or

BitwiseXor

Binary

BitwiseXor(a: type; b: type): resultType;

xor


No operators other than those listed in the table may be defined on a class or record.

 

以下是通過例項來演示

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;

 

執行結果如圖

可以看見非常靈活的實現各種操作,非常方便。