今天简单介绍一下 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. Pymysql部分

    安装: 1 执行SQL import pymysql # 创建连接 conn = pymysql.connect(host='172.30.2.233', port=3306, user='root' ...

  2. mysql-查询存在主表但是不存在副标的数据

    A.B两表,找出ID字段中,存在A表,但是不存在B表的数据.A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引. 方法一 使用 not in ,容易理解,效率低  ~执 ...

  3. JVM 垃圾回收GC Roots Tracing

    1.跟搜索算法: JVM中对内存进行回收时,需要判断对象是否仍在使用中,可以通过GC Roots Tracing辨别. 定义: 通过一系列名为”GCRoots”的对象作为起始点,从这个节点向下搜索,搜 ...

  4. 对象转Json时,Date类型格式化问题

    object是一个对象,该对象中有一个字段为Date类型 使用JSONObject obj = JSONObject.fromObject(object);将Object转成json时 Date类型字 ...

  5. python入门学习0

    Python 是什么类型的语言 Python是脚本语言 Python下载地址:https://www.python.org/downloads/ Python版本:Python 3.4.2 - 64b ...

  6. [Redis]Redis的五种数据类型与键值/服务器相关命令

    -------------------------------------------------------------------------------------- String(字符串):最 ...

  7. C# 反编译

    今儿也是运气背,不知怎么的,一脚就把电脑踢关机了(其实就轻轻碰到了一下主机),我去,写了一早上的代码,尼玛就不见,不见就算了,其实是保存了的,主要是文件还损坏了,尼玛,那心情!!! 然后就想着恢复,下 ...

  8. javaMail实现收发邮件(一)

    电子邮件的传输过程 电子邮件系统采用客户/服务器模式.电子邮件传送需要用到以下3个重要模块:MUA(Mail User Agent,邮件用户代理):用户通过它与电子邮件服务器打交道.MUA实际上就是邮 ...

  9. Windows驱动开发VS2012 DDK/WDK的环境配置

    [开发Windows驱动的配置是很必要的,下文将详细介绍VS2012如何配置驱动开发环境] [转载] 以下部分内容是转载博客:http://blog.csdn.net/huangxy10/articl ...

  10. Python设计模式 - UML - 包图(Package Diagram)

    简介 包图是对各个包及包之间关系的描述,展现系统中模块与模块之间的依赖关系.一个包图可以由任何一种UML图组成,可容纳的元素有类.接口.组件.用例和其他包等.包是UML中非常常用的元素,主要作用是分类 ...