delphi - 请求如何使用 TComplexMath 类的简单示例(包含源代码)

标签 delphi

我正在使用 Borland Delphi7 进行一些 Pascal 编程。我已经下载了一个相当基本(且免费)的复杂数学函数源代码库,但不幸的是它没有附带任何使用示例。由于我对 Pascal 中的类不是很熟悉,我想我只需要一个简单的例子来说明它的用法即可开始。

任何事情都可以,即使是将两个数字相加的例子也能让我开始。这是我尝试过的(我知道很蹩脚)。我认为我的问题是我不知道如何使用类构造函数。

uses ComplexMath in 'complexmath.pas'

var z1,z2,z3 : TComplexNumber;
begin
  z1.R:=1.0; z1.I:=2.0;
  z2.R:=3.0; z2.I:=-1.0;
  z3 := TComplexMath.Add(z1,z2);
end.

此处提供了 TComplexMath 的完整源代码:http://delphi.about.com/library/weekly/aa070103a.htm 。我还剪切并粘贴了下面的部分源代码列表(请注意,除了我明确指出它已被剪切的地方之外,此代码是完整的文件)。

部分 TComplexMath 源代码列表为:

unit ComplexMath;

interface

uses Windows, SysUtils, Classes, Controls, Math;

type
  TComplexNumber = record
    R : single;
    I : single;
  end;

TComplexMath = class(TComponent)
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner : TComponent); override;

    function Add(C1, C2 : TComplexNumber) : TComplexNumber; overload;
    { Returns the complex sum of C1 and C2 }

    function Add(C1, C2, C3 : TComplexNumber) : TComplexNumber; overload;
    { Returns the complex sum of C1 and C2 and C3 }

    ... and a bunch more like this ...

implementation

procedure Register;
  begin
    RegisterComponents('delphi.about.com', [TComplexMath]);
  end;

constructor TComplexMath.Create(AOwner : TComponent);
  begin
    inherited Create(AOwner);
  end;

 function TComplexMath.Add(C1, C2 : TComplexNumber) : TComplexNumber;
   begin
     Result.R := C1.R + C2.R;
     Result.I := C1.I + C2.I;
   end;

    ... and a bunch more like this ...

end.

经过一段时间的努力,我最终去掉了类定义,只使用函数本身(就像一个简单的函数库)。虽然这对我有用,但我知道这不是这个组件的用途。如果有人可以向我展示一个按照预期方式使用此类的非常简单的示例,我将非常感激。

最佳答案

预期用途是这样的:

var
  cm: TComplexMath;

  z, w, sum: TComplexNumber;
begin

  cm := TComplexMath.Create(Self) // or nil, can most likely be anything
  try
    sum := cm.Add(z, w); // Now sum is the sum of z and w
  finally
    cm.Free;
  end;

end;

您还可以在应用程序启动(或单元初始化等)时创建 TComplexMath 实例,并在应用程序的生命周期中使用它:

unit Unit1;

interface

var
  cm: TComplexMath;

...

implementation

procedure Test;
begin

  sum := cm.Add(z, w); // Now sum is the sum of z and w

end;

...

initialization

  cm := TComplexMath.Create(nil);

finialization

  cm.Free;

最后,由于它是一个组件,因此您可以在设计时将其拖放到表单上。该实例将被称为ComplexMath1,您可以在表单类中使用它,例如

procedure TForm1.Button1Click(Sender: TObject);
var
  z, w, sum: TComplexNumber;    
begin
  sum := ComplexMath1.Add(z, w);
end;

真的不喜欢这个类的设计。首先,您只需要它的一个实例,那么为什么不将函数改为类函数呢?真的:为什么要使用类呢?最后,如果您使用现代版本的 Delphi,则可以使用高级记录和运算符重载来使 z + w 之类的内容在源代码中工作,就像 zw 是简单类型,例如整数!

关于delphi - 请求如何使用 TComplexMath 类的简单示例(包含源代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15074316/

相关文章:

delphi - 仅关闭一小部分代码的 Delphi 范围检查

delphi - 如何通过单击空白背景来选择任何内容?

delphi - 在不同的 Delphi 安装上从 double 型到货币型的显式转换的不同行为

delphi - 为什么 Delphi XE7 IDE 因内存不足异常而挂起并失败?

delphi - DLL 中的 VCL 风格问题

delphi - 从 IShellLibrary 添加、删除文件夹

delphi - Ole Excel Delphi 日期问题

web-services - SOAP 网络服务中的 WS Security 1.2 Delphi XE8 for sign body

delphi - 无法将 TMyClass 对象分配给其基本接口(interface)类型的变量

delphi - 如何将 TImageList 中的图片分配给 TImage?