zoukankan      html  css  js  c++  java
  • delphi 中record 的类操作符重载简介

    今天简单介绍一下 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:

    OperatorCategoryDeclaration SignatureSymbol 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;
    复制代码

    运行结果如图

    可以看见非常灵活的实现各种操作,非常方便。

    https://www.cnblogs.com/xalion/p/9939314.html

  • 相关阅读:
    第16周作业
    第15周作业
    迟到的第14周作业
    第13次作业集
    软件工程结课总结
    第13次作业
    第12次作业
    第11次作业--字符串的处理
    第10次作业(2)
    第十次作业
  • 原文地址:https://www.cnblogs.com/findumars/p/10201381.html
Copyright © 2011-2022 走看看