⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 回调函数基础应用适用于回调函数初级应用
💻 PAS
字号:
unit Unit1;

interface

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

type
TFunType=function(i: Integer): Integer of object; {声明一个方法类型}


type
  TForm1 = class(TForm)
    Button1: TButton;
  function MyFun(i: Integer): Integer;
  function MyTest(x: Integer; F: TFunType): Integer;
  procedure Button1Click(Sender: TObject);        {建立类型兼容的函数}
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function tform1.MyFun(i: Integer): Integer;        {建立类型兼容的函数}
begin
  Result := i*2;
end;

{把函数当作参数, 再定义一个函数}
function tform1.MyTest (x: Integer; F: TFunType): Integer;
begin
  Result := F(x);
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  Fun: TFunType; {声明一个 TFunType 的变量}
  i: Integer;
begin
  Fun := MyFun;  {让方法变量 Fun 指向和它类型兼容的一个方法}

  {测试 Fun; Fun 是一个方法变量, 现在去执行那个方法, 它就可以当作那个方法来使用了}
  i := Fun(4);
  ShowMessage(IntToStr(i));  //8

  {把 Fun 当作参数使用; 把函数当作参数使用, 这就是回调函数}
  i :=Mytest(4,Fun);
  ShowMessage(IntToStr(i));  //8
end;

end.
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -