Delphi 之运算符重载
来源:互联网 发布:如何将程序导入单片机 编辑:程序博客网 时间:2024/05/22 14:19
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
- Delphi 之运算符重载
- Delphi的运算符重载
- 重载之运算符重载
- 运算符重载之(),++
- 运算符重载之->
- 运算符重载之 * ->
- 运算符重载之* ->
- 运算符重载之 []
- 重载运算符之->
- C++重载技术之运算符重载
- 运算符重载之等号
- 重载运算符之二
- 重载运算符之三
- C#之运算符重载
- C++之运算符重载
- C++之运算符重载
- C++之 运算符重载
- C++之运算符重载
- 一个简单的串口过滤驱动及一点体会
- .net程序中资源文件的保护办法探讨
- SQL Server里的 ISNULL 与 Oracle 中的 NULLIF
- SSH 端口转发
- 菜鸟提问:啊D注入工具 猜解出来的密码貌似不正确
- Delphi 之运算符重载
- Spring AOP声明式事务异常回滚
- Java 复习笔记_第4天
- 再见,谷歌
- 软件产品设计的五种境界
- 函数式编程另类指南1
- 谷歌中国大事记(不断更新中..)
- Java 复习笔记_第5天
- c#模拟平抛运动动画