unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
public
function MySqr(const num: Integer): Integer; {這是類中的一個方法, 准備做參數使用}
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
Type
TFun = function(const num: Integer): Integer of object; {定義的類型參數同上}
{這是上面那個方法的實現}
function TForm1.MySqr(const num: Integer): Integer;
begin
Result := num * num;
end;
{把上面的方法做參數}
procedure MyProc(var x: Integer; fun: TFun);
begin
x := fun(x);
end;
{測試}
procedure TForm1.FormCreate(Sender: TObject);
var
n: Integer;
begin
n := 9;
MyProc(n, MySqr);
ShowMessage(IntToStr(n)); {81}
end;
end.