delphi 精要-读书笔记(过程类型,方法类型)
2006-06-13 20:24
453 查看
下面是对两种数据类型的认识(过程类型,方法类型)
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,方法类型的变量只能通过对象来引用
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,方法类型的变量只能通过对象来引用
相关文章推荐
- Delphi:记录类型、过程指针、函数(方法)指针字段的应用
- SpringMVC确定目标方法POJO类型入参的过程
- Delphia5开发指南->第二章->方法的类型(2010年9月10日星期五)
- delphi adodataset1 调用存储过程的方法
- Delphi 函数指针 过程/函数类型
- InstallShield 工程类型MSI 使用过程中碰到的问题及解决方法。
- .net学习之.net和C#关系、运行过程、数据类型、类型转换、值类型和引用类型、数组以及方法参数等
- 《JavaScript高级程序设计》读书笔记之一:几个与原始类型等价的引用类型的常用方法和属性
- delphi 过程类型
- 【读书笔记】软件工程·实践者的研究方法第7版 第一部分 软件过程(引论 软件和软件工程)
- Delphi 函数指针 过程/函数类型
- Procedural Types (Delphi)Delphi中过程类型
- 用Delphi中的TADOQuery查询Oracle10G中的数据表时,Open时提示"数据类型不被支持"的处理方法
- 《Effective C#》读书笔记——条目5:为类型提供ToString()方法<C#语言习惯>
- 过程类型,方法类型
- Delphi 函数指针 过程/函数类型
- SpringMVC 确定目标方法 POJO 类型入参的过程
- 包含表值类型参数的存储过程调试方法
- Delphi 方法或过程中用的参数是动态数组的用法。
- Delphi枚举类型通过下标获取类型和通过类型获取下标方法