本文整理自网络,侵删。
其实 Delphi 2006 就已经支持运算符重载了, 不过 for Win32 的版本只是在 record 里支持.
运算符重载有什么用处呢? 举个例子:
譬如 "张三"、"李四" 两人一起来拜见你, 你可能会问: "你们两个谁大?"
其实你是在问: "你们两个谁的年龄大?"; 但生活中人们一般都不这么罗嗦.
在程序中能否这样简化呢? 这只能通过 "运算符重载" 做到!
先按 "你们两个谁的年龄大?" 写出原始例子吧:
--------------------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TMyRec = record
name: string;
age: Word;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
r1,r2: TMyRec;
str: string;
begin
r1.name := '张三'; r1.age := 18;
r2.name := '李四'; r2.age := 81;
if r1.age > r2.age then
str := r1.name
else
str := r2.name;
ShowMessageFmt('%s年龄大', [str]); {李四年龄大}
end;
end.
--------------------------------------------------------------------------------
上面是用 r1.age > r2.age 对比年龄, 如果能用 r1 > r2 来对比的话, 那就相当于: "你们两个谁大?" 了.
这样我们需要重载运算符 ">" ; 所谓重载运算符就是赋予某个运算符新的含义、新的功能.
我们可以重载的运算符只能是 Delphi 已有的运算符(还不是全部); C++ 也是这样.
需要用对应的指示字来表示这个运算符, 譬如: ">" 要用 GreaterThan 表示; C++ 不是这样.
重载运算符是通过方法实现的, 不过方法的指示字是 class operator (而非 function 或 proceture).
譬如上面的 record 可以这样声明:
type
TMyRec = record
name: string;
age: Word;
class operator GreaterThan(a,b: TMyRec): Boolean;
end;
还得有方法的实现(遗憾的是有些不能用 Ctrl+Shift+C 自动建立, 那就复制吧):
class operator TMyRec.GreaterThan(a,b: TMyRec): Boolean; {注意复制后再加上方法名: "TMyRec."}
begin
Result := a.age > b.age;
end;
下面是完整的代码:
--------------------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TMyRec = record
name: string;
age: Word;
class operator GreaterThan(a,b: TMyRec): Boolean;
end;
class operator TMyRec.GreaterThan(a,b: TMyRec): Boolean;
begin
Result := a.age > b.age;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
r1,r2: TMyRec;
str: string;
begin
r1.name := '张三'; r1.age := 18;
r2.name := '李四'; r2.age := 81;
if r1 > r2 then
str := r1.name
else
str := r2.name;
ShowMessageFmt('%s年龄大', [str]); {李四年龄大}
end;
end.
--------------------------------------------------------------------------------
这个测试里面有一些漏洞, 譬如: 年龄相等怎么办? 不必较真, 只是借此说明一个道理而已.
可重载运算符列表
类别 运算符 运算符标识 使用 转换 隐式转换 Implicit Implicit(a: type): resultType; 显式转换 Explicit Explicit(a: type): resultType; 一元 - Negative Negative(a: type): resultType; + Positive Positive(a: type): resultType; Inc Inc Inc(a: type): resultType; Dec Dec Dec(a: type): resultType not LogicalNot LogicalNot(a: type): resultType; not BitwiseNot BitwiseNot(a: type): resultType; Trunc Trunc Trunc(a: type): resultType; Round Round Round(a: type): resultType; 比较 = Equal Equal(a: type; b: type): Boolean; <> NotEqual NotEqual(a: type; b: type): Boolean; > GreaterThan GreaterThan(a: type; b: type) Boolean; >= GreaterThanOrEqual GreaterThanOrEqual(a: type; b: type): resultType; < LessThan LessThan(a: type; b: type): resultType; <= LessThanOrEqual LessThanOrEqual(a: type; b: type): resultType; 二元 + Add Add(a: type; b: type): resultType; - Subtract Subtract(a: type; b: type): resultType; * Multiply Multiply(a: type; b: type): resultType; / Divide Divide(a: type; b: type): resultType; div IntDivide IntDivide(a: type; b: type): resultType; mod Modulus Modulus(a: type; b: type): resultType; shl LeftShift LeftShift(a: type; b: type): resultType; shr RightShift RightShift(a: type; b: type): resultType; and LogicalAnd LogicalAnd(a: type; b: type): resultType; or LogicalOr LogicalOr(a: type; b: type): resultType; xor LogicalXor LogicalXor(a: type; b: type): resultType; and BitwiseAnd BitwiseAnd(a: type; b: type): resultType; or BitwiseOr BitwiseOr(a: type; b: type): resultType; xor BitwiseXor BitwiseXor(a: type; b: type): resultType;
相关阅读 >>
Delphi 网上获取北京时间firedac 下的 sqlite [3] - 获取数据库的基本信息
Delphi cxdbtreelist:最简单的节点图标添加方法
更多相关阅读请进入《Delphi》频道 >>