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 android gps服务

Delphi idhttp上传图给asp完美解决

Delphi获取千千静听歌词下载地址源码

Delphi定位注册表指定键位

Delphi downloadtomemory

Delphi xe10实现移动端支付宝、微信支付接口

Delphi superobject json操作类的基本用法

Delphi自有的md5函��

Delphi中formatdatetime函数的用法

Delphi idhttp post中文的问题

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



打赏

取消

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

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

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

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

评论

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