Delphi中的“委託”

 .NET中有委託(Delegate)的概念,其聲明形式如下所示:

 
  public delegate void MyDelegate(int aIntParam, string aStringParam);
 
  依個人所見,委託實際上就是規定一種接口,提供一種規範,任何符合該委託簽名的函數/過程都屬於同一類。
 
  在Delphi中,也有類似於“委託”的概念(不過可沒有C#的功能豐富,不過兩者從根本上說都應該是函數指針),如下所示:

 
  type
    TMyDelegateFunc = function (AIntParam: integer; AStringParam: string): Boolean;
    TMyDelegateProc = procedure (AIntParam: integer; AStringParam: string);
 
  在以上的聲明中,還可以用of object關鍵字來規定所定義的“委託”是應用於對象的函數/過程,還是應用於非對象的函數/過程,例:
 
  type
    TMyObjectDelegate = procedure (AIntParam: integer; AStringParam: string) of object; //對象的
函數/過程
    TMyRegularDelegate = procedure (AIntParam: integer; AStringParam: string); //非對象的(一般的)函數/過程
 
  以下舉個簡單的例子來說明一下Delphi中“委託”的應用。附件爲完整程序。
  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章