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,统一复数 ...
随机推荐
- 在WINDOWS7下无U盘安装WINDOWS10
第一步:下载cn_windows_10_business_editions_version_1803_updated_march_2018_x64_dvd_12063730.iso 第二步:解压里面的 ...
- oracle之分析函数解析及其应用场景
ORACLE 分析函数FIRST_VALUE,LAST_VALUE用法sum overavg over first_value overlast_value over...聚合函数结合over就是分析 ...
- ORACLE日常操作手册
转发自:http://blog.csdn.net/lichangzai/article/details/7955766 以前为开发人员编写的oracle基础操作手册,都基本的oracle操作和SQL语 ...
- java细节知识
代码优化细节 (1)尽量指定类.方法的final修饰符 带有final修饰符的类是不可派生的.在Java核心API中,有许多应用final的例子,例如java.lang.String,整个类都是fin ...
- HTML5 元素文字超出部分显示省略号(支持多行),兼容几乎所有常用浏览器
1,公共样式,在公共的 CSS 文件中加入以下内容 /* 超出部分显示省略号,支持多行 */ .text-ells:before { content: ''; float: left; width: ...
- leetcode300
本题使用回溯法,深度优先搜索.使用隐式条件来进行加速. public class Solution { ; int[] x; Dictionary<int, int> dic = new ...
- pyqt---------事件与信号处理
pyqt:信号与槽的关系 GUI应用程序是事件驱动的. 事件主要由应用程序的用户生成. 但它们也可以通过其他手段产生,例如:网络连接,窗口管理器或定时器. 当我们调用应用程序的exec_()方法时,应 ...
- synchronized 和reentrantlock的优缺点
reentrantlock的优点 可以添加多个检控条件, 如果使用synchronized,则只能使用一个. 使用 reentrant locks 可以有多个wait()/notify() 队列. [ ...
- 深度学习VS机器学习——到底什么区别
转自:https://baijiahao.baidu.com/s?id=1595509949786067084&wfr=spider&for=pc 最近在听深度学习的课,老师提了一个基 ...
- jeesite 下载ckfinder上传的文件
在需要下载的位置,将以下代码复制到页面最下方,就可以实现文件下载了 <script> $(document).ready(function() { var fileName = $(&qu ...