Delphi 之运算符重载

本文介绍Delphi7及以后版本中引入的运算符重载功能,通过实例展示了如何为自定义类型实现加法运算符,并列出了Delphi支持的所有运算符重载及其对应的英文标识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Delphi 7之后的版本,增加了运算符的重载。虽然不尽人意(需要写特定英文),但有总比没有强。

例:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  T3DPoint = record
    X, Y, Z: Double;
    class operator ADD(ALeftPoint, ARightPoint: T3DPoint): T3DPoint; {重载'+'运算符}
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ T3DPoint }

class operator T3DPoint.ADD(ALeftPoint, ARightPoint: T3DPoint): T3DPoint;
begin
  Result.X := ALeftPoint.X + ARightPoint.X;
  Result.Y := ALeftPoint.Y + ARightPoint.Y;
  Result.Z := ALeftPoint.Z + ARightPoint.Z;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  A, B, Z: T3DPoint;
begin
  A.X := 1; A.Y := 1; A.Z := 1;
  B.X := 3; B.Y := 3; B.Z := 3;
  Z := A + B;

  ShowMessageFmt('%f, %f, %f', [Z.X, Z.Y, Z.Z]); {4.00, 4.00, 4.00}
  //ShowMessage(FloatToStr(Z.X) + ',' + FloatToStr(Z.Y) + ',' + FloatToStr(Z.Z)); {4, 4, 4}
end;

end.

 

下面附上所有的运算符:

     重载特定英文                        行为(或目数)    方法                                                                                 操作符

  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
  BitwiseNot  Unary  BitwiseNot(a: type): resultType;  not
  Trunc  Unary  Trunc(a: type): resultType;  Trunc
  Round  Unary  Round(a: type): resultType;  Round
  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): resultType;  >=
  LessThan  Comparison  LessThan(a: type; b: type): resultType;  <
  LessThanOrEqual  Comparison  LessThanOrEqual(a: type; b: type): resultType;  <=
  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

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值