小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Delphi 2009 中的匿名方法(reference to)

 獨孤求財 2012-03-30

之前我們可以定義方法類型, 然后通過方法類型的變量來使用方法, 譬如:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Type
  TFun = function(const num: Integer): Integer; {先定義一個方法類型}

  function MySqr(const num: Integer): Integer;  {再創(chuàng)建一個吻合上面類型的一個方法}
  begin
    Result := num * num;
  end;

{測試}
procedure TForm1.FormCreate(Sender: TObject);
var
  fun: TFun;  {方法變量}
  n: Integer;
begin
  fun := MySqr;             {給變量賦值為相同格式的方法}
  n := fun(9);              {現(xiàn)在這個方法變量可以使用了}
  ShowMessage(IntToStr(n)); {81}
end;

end.

之所以這樣做, 是因為有時需要把 "方法" 當作參數(shù), 譬如:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Type
  TFun = function(const num: Integer): Integer; {先定義一個方法類型}

  function MySqr(const num: Integer): Integer;  {再創(chuàng)建一個吻合上面類型的一個方法}
  begin
    Result := num * num;
  end;

  {把方法當作參數(shù)的方法}
  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.

現(xiàn)在 Delphi 2009 可以使用匿名方法了(使用 reference 定義方法類型, 然后在代碼中隨用隨寫方法), 譬如:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Type
  TFun = reference to function(const num: Integer): Integer; {用 reference 定義匿名方法類型}

procedure TForm1.FormCreate(Sender: TObject);
var
  fun: TFun;
  n: Integer;
begin
  {求平方}
  fun := function(const a: Integer): Integer {注意本行最后不能有 ; 號}
  begin
    Result := a * a;
  end;

  n := fun(9);
  ShowMessage(IntToStr(n)); {81}

  {求倍數(shù)}
  fun := function(const a: Integer): Integer
  begin
    Result := a + a;
  end;

  n := fun(9);
  ShowMessage(IntToStr(n)); {18}
end;

end.

把匿名方法當作其他方法的參數(shù):
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Type
  TFun = reference to function(const num: Integer): Integer;

  function FunTest(const n: Integer; fun: TFun): string;
  begin
    Result := Format('%d, %d', [n, fun(n)]);
  end;

procedure TForm1.FormCreate(Sender: TObject);
var
  f: TFun;
  s: string;
begin
  f := function(const a: Integer): Integer {注意本行最后不能有 ; 號}
  begin
    Result := a * a;
  end;

  s := FunTest(9, f);

  ShowMessage(s); {9, 81}
end;

end.

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多