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 twebbrowser控件编程

Delphi twebbrowser出现 method pastehtml not supported by automation object 解决方法

Delphi xe5 程序中标识win max android ios程序代码分别实现

Delphi遍历指定目录下指定类型文件的函数

Delphi 开发安卓时判断进入非活动

delpni-xe5-android sdk api 层次结构

dll 的静态调用实例代码

Delphi copy 字符串函数

Delphi idhttp超时重定向

Delphi 关于字符串的一些使用技巧

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



打赏

取消

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

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

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

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

评论

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