Delphi>请解释一下:类型…对象的过程

前端之家收集整理的这篇文章主要介绍了Delphi>请解释一下:类型…对象的过程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我遇到了一些对我来说很新的代码……

我从来没有真正看到过对象过程的类型声明,我只是没有
明白这一点.
为什么开发人员不能只保留Boolean类型的字段?

interface

    type
      TFinishedCaptureEvent = procedure(AFinished: Boolean) of object;

      TFrameCard = class(TFrame)
      ...
      private
        FOnFinishedCapture: TFinishedCaptureEvent;
        procedure DoUpdateMessage(AMessageType: TMessageType);
      public
        property OnFinishedCapture: TFinishedCaptureEvent read FOnFinishedCapture write FOnFinishedCapture;
      end;    

implementation

...

procedure TFrameCard.DoUpdateMessage(AMessageType: TMessageType);
begin
  if Assigned(FOnFinishedCapture) then
    FOnFinishedCapture(False);
  ...
end;

end.

解决方法

对象的过程是类实例中包含的过程的过程参考.调用作为类成员的过程时,必须使用其他参数传递implict Self引用.使用对象的过程告诉编译器将Self引用与过程引用中的过程地址一起存储,这样当通过过程引用调用过程时,将自动传递Self引用.

在您提供的代码片段中,TFinishedCaptureEvent被定义为对象的过程,这意味着其类型创建的任何变量将包含2个值:Self值和过程地址.分配此变量时,特别是当赋值在类中时,编译器将自动将此变量内的Self值设置为包含分配给变量的过程的类的实例.调用变量(FOnFinishedCapture(False))时,编译器会自动将正确的Self值传递回分配给此变量的过程.

原文链接:https://www.f2er.com/delphi/102127.html

猜你在找的Delphi相关文章