delphi 如何把类中的方法做参数


本文整理自网络,侵删。

 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;

{把 TFun 类型的方法做参数}
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.

相关阅读 >>

Delphi中简单的调用单元unit实例

Delphi中实现dbgrid列宽度自动调整

Delphi中inputbox 和inputquery 函数的使用

Delphi tnethttpclient https忽略证书验证

Delphi hide

Delphi unidac 连接mdb access 数据库

Delphi �c 如何使用datasnap获取作为标头传递的标记?

Delphi 使format输出百分号 %

Delphi下获取系统默认的useragent的方法

Delphi 以二进制方式读取图片保存到string

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...