delphi 中record 的类操作符重载简介

摘要:
今天,我将简要介绍delphi中记录类运算符的重载,即如何实现记录之间的简单操作。对于类运算符重载,可以参考官方文档。

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

运行结果如图

delphi 中record 的类操作符重载简介第1张

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

免责声明:文章转载自《delphi 中record 的类操作符重载简介》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇重磅!JDK 17 发布,Oracle 宣布从 JDK 17 开始正式免费。。AD与LDAP区别下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

delphi 7 mdi子窗体。。。无法更改以命令对象为源的记录集对象的 ActiveConnection 属性。

问题是这样的 我做了一个小程序 把 adoconnection放到了主窗体  连接的是access数据库; 新建了一个子窗体继承自FBase  新建了一个pubulic方法 qrySearch 实现了打开表; formCreate调用了qrySearch方法 ; public procedure qrySearch(cLiuShui: stri...

delphi 操作xml示例(DelphiBBS)

自:http://www.delphibbs.com/keylife/iblog_show.asp?xid=20713 ================================================================ 2005-9-23 21:05:34xml基础操作实例,因为刚开始学,如果有不对的地方,请批评指正,代码...

10分钟10行代码开发APP(delphi 应用案例)

总结一下用到的知识(开发环境安装配置不计算在内): 第六章 使用不同风格的按钮; 第十七章 让布局适应不同大小与方向的窗体; 第二十五章使用 dbExpress访问 InterBase ToGo 第二十九章 从移动客户端连接到企业数据库; 以下是具体制作步骤: 1、创建DataSnap服务VCL应用程序 1.1 创建服务程序 新建一个项目:D...

Delphi下EasyGrid使用体会

最近在编写软件的时候,非常需要一款支持多表头的StringGrid控件,朋友介绍使用EasyGrid控件,这款控件大概从04年开始就没有再更新,网上有关与它的资料也较少。但是通过其demo,此软件还是能满足需要,由于资料少,所以将自己在使用过程中的一些体会整理出来。 一、多表头合并 在EasyGrid中提供了SetMerges方法用于多表头合并,该方法原型...

Delphi 2009 之 TBalloonHint

本例相关图片: 代码文件: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ImgList; type TForm1 = class(TForm)...

delphi 集合类型探究

集合是由具有某些共同特征的元素构成的一个整体。在pascal中,一个集合是由具有同一有序类型的一组数据元素所组成,这一有序类型称为该集合的基类型。  一、集合类型的定义和变量的说明  集合类型的一般形式为:    set of 基类型;  基类型可以是任意顺序类型, 而不能是实型或其它构造类型。同时,基类型的数据的序号不得超过255。例如下列说明是合法的:...