问题1:
现在有一个类TA,提供一个Process方法。
unit A;
interface
type
TA = class
public
procedure Process();
end;
implementation
procedure TA.Process();
begin
//循环n次
end;
现在我需要提供一个LoopOne事件,每做一次循环触发一下这个事件。应该如何处理。问题2:
在问题1解决的基础上,现在我定义类TB,定义一个test方法,在test方法中调用了类TA的Process方法
现在我需要处理TA的LoopOne事件,应该如何写?
unit B;
interface
type
TB = class
public
procedure Test();
end;
implementation
prcedure TB.Test();
var
ca:TA;
begin
ca := TA.Create();
ca.Process();
ca.Destroy();
end;
现在有一个类TA,提供一个Process方法。
unit A;
interface
type
TA = class
public
procedure Process();
end;
implementation
procedure TA.Process();
begin
//循环n次
end;
现在我需要提供一个LoopOne事件,每做一次循环触发一下这个事件。应该如何处理。问题2:
在问题1解决的基础上,现在我定义类TB,定义一个test方法,在test方法中调用了类TA的Process方法
现在我需要处理TA的LoopOne事件,应该如何写?
unit B;
interface
type
TB = class
public
procedure Test();
end;
implementation
prcedure TB.Test();
var
ca:TA;
begin
ca := TA.Create();
ca.Process();
ca.Destroy();
end;
unit A;
interface
type
TA = class
private
proceudre LoopOne;
public
procedure Process;
end;
implementation
procedure TA.Process();
begin
//循环n次
LoopOne;
end;2:
现在我需要处理TA的LoopOne事件,应该如何写?
不明白你说的
unit A;
interface
type
TA = class
private
proceudre LoopOne;
public
procedure Process;
end;
implementation
procedure TA.Process();
begin
//循环n次
LoopOne;
end;2:
现在我需要处理TA的LoopOne事件,应该如何写?
不明白你说的
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Mask;type
TTT = procedure(i:Integer) of object; TA = class
private
FEvent1:TTT;
Published
Property Event1:TTT Read FEvent1 Write FEvent1;
public
procedure Process(n:Integer);
end; TB = class
private
procedure zz(i:Integer);
public
procedure Test();
end;implementation{ TB }procedure TB.Test;
var
ca:TA;
begin
ca := TA.Create();
ca.Event1:=zz;
ca.Process(3);
ca.Free();//不要直接调用Destroy
end;procedure TB.zz(i:Integer);
begin
ShowMessage('Event '+IntToStr(i));
end;{ TA }procedure TA.Process(n:Integer);
var
i:Integer;
begin
for i:=1 to n do
if Assigned(FEvent1) then FEvent1(i);
end;end.
interface
type
TA = class
private
FOnLoop: TNotifyEvent;
procedure DoLoop;
public
procedure Process();
property OnLoop: TNotifyEvent read FOnLoop write FOnLoop;
end;implementation
procedure TA.Process();
begin
DoLoop;
end;procedure TA.DoLoop
begin
if Assigned(FOnLoop) then FOnLoop(Sender);
end;class TB
private
procedure OnLoop(Sender: TObject);
public
procedure Test;
end;prcedure TB.Test();
var
ca:TA;
begin
ca := TA.Create();
TA.OnLoop := OnLoop; // 这里
ca.Process();
ca.Destroy();
end;type TNotifyEvent = procedure (Sender: TObject) of object; // 这是 delphi 的 TNotifyEvent 定义
参数可以任意, 注意响应函数中参数要跟事件类型定义的相同