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:
| 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 的类操作符重载简介的更多相关文章
- Delphi中的线程类 - TThread详解
Delphi中的线程类 - TThread详解 2011年06月27日 星期一 20:28 Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大多数Delphi书藉都有说到,但基本 ...
- Delphi中的线程类(转)
Delphi中的线程类 (转) Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大多数Delphi书藉都有说到,但基本上都是对 TThread类的几个成员作一简单介绍,再说明一下 ...
- delphi中Record 和Packed Record的区别
Record 和Packed Record 第一种不带packed关键字的结构体表明编译器编译时要求进行字对齐,而第二种带packed关键字的结构体表明编译器编译该结构体时不需要进行字对齐,这种方式对 ...
- Delphi中record和packed record的区别
转载:http://blog.csdn.net/rznice/article/details/6566978 第一种不带packed关键字的结构体表明编译器编译时要求进行字对齐. 而第二种带packe ...
- Delphi中获取某类的祖先类及其所在单元名称(使用GetTypeData(PClass.ClassInfo)函数,并且该类是从TPersistent类的派生类才可以这么使用)
前几天在CSDN社区看到一篇<如何得到自身单元名称>的帖子,其中一位名为sdzeng网友给出了答案.受此启发,自己写了一个函数,用来获取指定类的所有祖先类的名称及其所在的单元名称. //参 ...
- C++中,用类和重载运算符写高精模板
先放代码: #include<iostream> #include<cstdio> #include<cstring> using namespace std; s ...
- Delphi 中的 XMLDocument 类详解(10) - 判断节点类型: 支节点、叶节点、文本节点、空节点
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- Delphi 中的 XMLDocument 类详解(9) - 关于 HasChildNodes 与 IsTextElement
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- C++中采用操作符重载完善复数类
1,复数类应该具有的操作: 1,运算:+,-,*,/: 2,比较:==,!=: 3,赋值:=: 4,求模:modulus: (5),完善的复数类操作符重载必不可少: 2,利用操作符重载: 1,统一复数 ...
随机推荐
- ubuntu crontab python 定时任务备记
crontab -e 写入: # at a.m every week with: # * * tar -zcf /var/backups/home.tgz /home/ # # For more in ...
- Numpy学习笔记(一)
(1)NumPy的核心对象 ndarray 用于表示N 维数组类型.它描述相同类型的元素集合. 可以使用基于零的索引访问集合中的项目. (2)Ndarray的创建 可以使用numpy.array() ...
- WPF dev 获取gridControl筛选后的数据
GridControl.DataController.GetAllFilteredAndSortedRows();
- BZOJ 4584 luogu P3643: [Apio2016]赛艇
4584: [Apio2016]赛艇 Time Limit: 70 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 在首尔 ...
- 【Linux】【Selenium】安装Chrome和ChromeDriver的配置
转自:https://www.cnblogs.com/longronglang/p/8078898.html 1.安装chrome sudo apt-get install libxss1 libap ...
- redis 10个问题
(1)什么是redis? Redis 是一个基于内存的高性能key-value数据库. (有空再补充,有理解错误或不足欢迎指正) (2)Reids的特点 Redis本质上是一个Key-Value类型的 ...
- XAMPP与本地Mysql冲突解决方法
1.更改regeit目录 https://blog.csdn.net/sinat_37633633/article/details/77645463 2.更改配置文件my.ini (1)https:/ ...
- 【Java】获取二维数组行列长度
二维数组int array[][] = new int[3][3]; 行长度:array.length 列长度:array[i].length
- [INet] WebSocket 数据收发的详细过程
WebSocket 和 HTTP 相似,只是一个应用层协议,对下层透明,所以不涉及 TCP/IP. 由于浏览器支持了 WebSocket,所以在用 JS 写客户端的时候,是无需考虑数据的编码解码的. ...
- 配置远程主机http服务器 打包资源
<1> 搭建nginx 验证nginx是否启动成功 https://blog.csdn.net/wdsdsdsds/article/details/51179780 https://ww ...