delphi 精要-读书笔记(过程类型,方法类型)

news/2024/5/18 22:07:53 标签: delphi, 读书, integer, button, forms, function

下面是对两种数据类型的认识(过程类型,方法类型)

1.过程类型

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
type
    TOneFun=function(X:Integer):Integer;

function SomeFunction(X:Integer):Integer;
begin
   Result:=X*2
end;

function SomeCallBack(X:Integer;OneFun:TOneFun):Integer;  //这个相当于一个回调函数
begin
   Result:=OneFun(X);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
   F:TOneFun;
   I,J:Integer;
begin
   F:=SomeFunction;
   I:=F(4);
   j:=SomeCallBack(4,F);
   if i=j then
     showmessage('F(4)和SomeCallBack功能相同');
     showmessage(inttostr(i));
     showmessage(inttostr(j));
end;

end.

2.方法类型

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure ShowInfo;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{
  方法指针可以用定义在System单元的一个记录描述
  type
    TMethod=record
    code,data :Pointer;

  它包含两个指针code和data,code可以看作是方法地址的指针,data可以看做是方法所属对
  象的指针
}

procedure TForm1.Button1Click(Sender: TObject);
type
   TMyProcedure=procedure of object;  //定义了一个方法类型
var
   OneProcedure:TMyProcedure;  //声明一个方法类型的变量
begin
   OneProcedure:=Form1.ShowInfo;  //给方法指针赋值
   {
    也可以这样给方法指针赋值
    TMethod(OneProcedure).code:=Form1.MethodAddress('showinfo');
    TMethod(OneProcedure).data:=Form1;
   }
   ShowMessage(TObject(TMethod(OneProcedure).Data).ClassName);
   OneProcedure;
end;

procedure TForm1.ShowInfo;
begin
   ShowMessage(Self.Name);
end;

end.

过程类型的变量是指向过程的指针,和回调函数差不多,方法类型的变量是指向方法的指针,写法上还比过程类型多了 of objects,方法类型的变量只能通过对象来引用


http://www.niftyadmin.cn/n/1416133.html

相关文章

RedHat6.3配置DNS服务器

2019独角兽企业重金招聘Python工程师标准>>> RedHat6.3配置DNS服务器 作者:沈小然 北京证联支付有限责任公司 1 系统环境 OS:Red Hat Enterprise Linux Server release 6.3 (Santiago) DNS 服务器:172.21.20.1/255.…

mysql左右连接举例_mysql左连接内连接闲谈

最近忙着开发x省冷链追溯系统,天天干到晚上十一点多才回到家,周末也加班,没啥时间写博客,闲下来再好好写写业务: sql语句统计出入库数据。问题: 只统计了X端入库单。原因: 没有发现X端的数据库中…

【XSY2988】取石子 博弈论

题目描述 有 \(n\) 堆石子,每堆石子的个数是 \(c_i\)。 Alice 和 Bob 轮流取石子(先后手未定),Alice 每次从一堆中取 \(a\) 个,Bob每次从一堆中取 \(b\) 个。无法操作者输。 你要选定若干堆石子(共 \(2^n\)…

python守护进程 限制调用频率_Python守护进程定期调用子进程

我正在基于Sander Marechals代码构建一个简单的pyhon守护程序。守护进程的全部目的是每秒运行一个php文件(php文件通过数据库循环检查值和更新数据库)。该部分出现问题subprocess.call([php,test.php])我可以运行“php测试.php“在shell上,它执行它应该做的事情&…

delphi 精要-读书笔记(内存分配释放)

1.内存分为三个区域:全局变量区,栈区,堆区 全局变量区:专门存放全局变量 栈区:分配在栈上的变量可被栈管理器自动释放 堆区:堆上的变量内存必须人工去释放 2.指针类变量 指针类的变量在声明为全局变量…

卷积神经网络--猫狗系列【VGG16】

数据集:【文末】 ​ 数据集预处理 定义读取数据辅助类(继承torch.utils.data.Dataset) import osimport PILimport torchimport torchvisionimport matplotlib.pyplot as pltimport torch.utils.dataimport PIL.Image # 数据集路径train_p…

Ball Coloring

6552: Ball Coloring 时间限制: 1 Sec 内存限制: 128 MB提交: 13 解决: 7[提交][状态][讨论版][命题人:admin]题目描述 There are N bags, each containing two white balls. The i-th box contains two balls with integers xi and yi written on them, respectively.For ea…

java链入到mysql_java链接到mysql

当然,首先要安装有JDK(一般是JDK1.5.X)。然后安装MySQL,这些都比较简单,具体过程就不说了。配置好这两个环境后,下载JDBC驱动mysql-connector-java-5.0.5.zip(这个是最新版的)。然后将其解压缩到任一目录。我是解压到D盘&#xff…