今天简单介绍一下 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:=;
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:=; //等于整数
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:=;
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+; //加整数 memo1.Lines.Add('myrec ival='+ myrec.ival.ToString);
memo1.Lines.Add('myrec dval='+ formatdatetime('yyyy-mm-dd',myrec.dval));
memo1.Lines.Add('>>>>>>>>>>>>>>>>>>end<<<<<<<<<<<<<<<<'); rec2:=; 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中的线程类 - TThread详解

    Delphi中的线程类 - TThread详解 2011年06月27日 星期一 20:28 Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大多数Delphi书藉都有说到,但基本 ...

  2. Delphi中的线程类(转)

    Delphi中的线程类 (转) Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大多数Delphi书藉都有说到,但基本上都是对 TThread类的几个成员作一简单介绍,再说明一下 ...

  3. delphi中Record 和Packed Record的区别

    Record 和Packed Record 第一种不带packed关键字的结构体表明编译器编译时要求进行字对齐,而第二种带packed关键字的结构体表明编译器编译该结构体时不需要进行字对齐,这种方式对 ...

  4. Delphi中record和packed record的区别

    转载:http://blog.csdn.net/rznice/article/details/6566978 第一种不带packed关键字的结构体表明编译器编译时要求进行字对齐. 而第二种带packe ...

  5. Delphi中获取某类的祖先类及其所在单元名称(使用GetTypeData(PClass.ClassInfo)函数,并且该类是从TPersistent类的派生类才可以这么使用)

    前几天在CSDN社区看到一篇<如何得到自身单元名称>的帖子,其中一位名为sdzeng网友给出了答案.受此启发,自己写了一个函数,用来获取指定类的所有祖先类的名称及其所在的单元名称. //参 ...

  6. C++中,用类和重载运算符写高精模板

    先放代码: #include<iostream> #include<cstdio> #include<cstring> using namespace std; s ...

  7. Delphi 中的 XMLDocument 类详解(10) - 判断节点类型: 支节点、叶节点、文本节点、空节点

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  8. Delphi 中的 XMLDocument 类详解(9) - 关于 HasChildNodes 与 IsTextElement

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  9. C++中采用操作符重载完善复数类

    1,复数类应该具有的操作: 1,运算:+,-,*,/: 2,比较:==,!=: 3,赋值:=: 4,求模:modulus: (5),完善的复数类操作符重载必不可少: 2,利用操作符重载: 1,统一复数 ...

随机推荐

  1. redis总结问题

    简单回顾了redis,在这过程中 首先得了解redis是什么,redis的运用场景,redis支持哪些数据格式,redis如何操作数据,redis如何实现高可用 redis是什么: Redis 是一个 ...

  2. Python发送微信消息

    针对此 需要安装itchat第三方模块 采用pip安装就可以了   pip install itchat import itchatimport time#引入时间函数进行测试time_format= ...

  3. keil5 MDK 链接报错 Error: L6410W 解决

    keil5 MDK 报错 Build target 'Project' linking... .\Output\Project.axf: Warning: L6310W: Unable to find ...

  4. linux6下源码安装mysql5.6

    概述:CentOS 6.4下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.14.正文:一:卸载旧版本使用下面的命令检查是否安装有MySQL Serverrpm ...

  5. hive的使用

    1,前言 书接上回,接着对上一个hive安装后环境下的hive使用.

  6. Java内存泄漏的几种可能

    Java内存泄漏引起的原因: 内存泄漏是指无用对象(不再使用的对象)持续占有内存或无用对象的内存得不到及时释放,从而造成内存空间的浪费称为内存泄漏. 长生命周期的对象持有短生命周期对象的引用就很可能发 ...

  7. Visual Studio资源汇总

    Visual Studio 2015:http://tieba.baidu.com/p/3442930798Visual Studio 2013:http://tieba.baidu.com/p/34 ...

  8. 项目(三)PXE高效能批量网络装机

    PXE:预启动执行环境 PXE是由intel公司开发的网络引导技术,工作在Client/Server模式,允许客户机通过网络从远程服务器下载引导镜像,并加载安装文件或者整个操作系统. 若要搭建PXE网 ...

  9. Binary Space Partitioning

    [Binary Space Partitioning] BSP was discovered by John Carmack used BSP trees in Doom and Quake. Alt ...

  10. 用命令生成Webservice 对应的代理类

    wsdl /language:C# /namespace:Camstar.WebPortal.WebPortlets.Shopfloor.SAP.GreatWall /out:gwSAPService ...