delphi 运算符重载

譬如上面的 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;

 

Delphi for Win32 只支持 record 类型的运算符重载,而 Delphi .NET 还支持 class 类型的运算符重载
下面是Delphi支持的可重载运算符

运算符       类型       声明语法                                          应用符号
Implicit     转换       Implicit(a: type): resultType;                    隐式转换

Explicit     转换       Explicit(a: type): resultType;                    显式转换

负           一元运算   Negative(a: type): resultType;                    -

正           一元运算   Positive(a: type): resultType;                    +

递增         一元运算   Inc(a: type): resultType;                         Inc

递减         一元运算   Dec(a: type): resultType;                         Dec

逻辑非       一元运算   LogicalNot(a: type): resultType;                  not

按位非       一元运算   BitwiseNot(a: type): resultType;                  not

截取         一元运算   Trunc(a: type): resultType;                       Trunc

舍入         一元运算   Round(a: type): resultType;                       Round

等于         比较       Equal(a: type; b: type) : Boolean;                =

不等于       比较       NotEqual(a: type; b: type): Boolean;              <>

大于         比较       GreaterThan(a: type; b: type) Boolean;            >

大于等于     比较       GreaterThanOrEqual(a: type; b: type): resultType;>=

小于         比较       LessThan(a: type; b: type): resultType;           <

小于等于     比较       LessThanOrEqual(a: type; b: type): resultType;    <=

加           二元运算   Add(a: type; b: type): resultType;                +

减           二元运算   Subtract(a: type; b: type): resultType;           -

乘           二元运算   Multiply(a: type; b: type): resultType;           *

除           二元运算   Divide(a: type; b: type): resultType;             /

整除         二元运算   IntDivide(a: type; b: type): resultType;          div

模           二元运算   Modulus(a: type; b: type): resultType;            mod

左移         二元运算   ShiftLeft(a: type; b: type): resultType;          shl

右移         二元运算   ShiftRight(a: type; b: type): resultType;         shr

逻辑与       二元运算   LogicalAnd(a: type; b: type): resultType;         and

逻辑或       二元运算   LogicalOr(a: type; b: type): resultType;          or

逻辑异或     二元运算   LogicalXor(a: type; b: type): resultType;         xor

按位与       二元运算   BitwiseAnd(a: type; b: type): resultType;         and

按位或       二元运算   BitwiseOr(a: type; b: type): resultType;          or

按位异或     二元运算   BitwiseXor(a: type; b: type): resultType;         xor

转载于:https://www.cnblogs.com/h2zZhou/p/11444600.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值