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 |