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中使用ado连接带密码的access

Delphi 网上获取北京时间验证码识别之中值滤波

Delphi过程函数传递参数的八种方式

Delphi简单判断程序30秒没有键盘和鼠标动作示例

Delphi 动态创建窗体

Delphi xe10.x 快捷失灵解决办法

Delphi win10系统通知 notificationcenter1 基本用法

Delphi xe5的新功能“ tlistview内置搜索过滤”

Delphi以鼠标为中心缩放图片以及拖动(采用区域抠图拉伸的方式)

Delphi环境下基于spcomm控件开发串口通讯报文字节丢失的问题解决

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



打赏

取消

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

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

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

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

评论

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