灯网一家专门做灯的网站,盘锦网站建设公司,购物网站建设优缺点,建设网站的源代码12.1.4 类静态方法与Windows API回调
静态类方法没有隐藏的Self参数意味着静态类方法可以作为回调函数传递给操作系统#xff08;例如#xff0c;在Windows上#xff09;。实际上#xff0c;您可以声明一个具有stdcall调用约定的静态类方法#xff0c;并将其用作直接的…12.1.4 类静态方法与Windows API回调
静态类方法没有隐藏的Self参数意味着静态类方法可以作为回调函数传递给操作系统例如在Windows上。实际上您可以声明一个具有stdcall调用约定的静态类方法并将其用作直接的Windows API回调就像我在StaticCallBack示例中的TimerCallBack方法中所做的那样
type TFormCallBack class(TForm) ListBox1: TListBox; procedure FormCreate(Sender: TObject); private class var NTimerCount: Integer; public procedure AddToList (const AMessage: string); class procedure TimerCallBack(hwnd: THandle; uMsg, idEvent, dwTime: Cardinal); static; stdcall; end; 类数据被用作回调计数器。OnCreate处理器通过将地址传递给静态类过程来调用SetTimer API
procedure TFormCallBack.FormCreate(Sender: TObject);
var Callback: TFNTimerProc;
begin NTimerCount : 0; Callback : TFNTimerProc(TFormCallBack.TimerCallBack); SetTimer(Handle, TIMERID, 1000, Callback);
end;注解TFNTimeProc的参数是一个方法指针这就是为什么方法的名称要以开头或者通过使用Addr函数来获取方法地址。这是因为我们需要获取方法的地址而不是执行方法。 现在实际的回调函数增加了计时器并更新了窗体通过引用相应的全局变量来指代它——这应该避免但需要在演示中增加一些复杂性因为类方法不能将窗体作为Self引用
class procedure TFormCallBack.TimerCallBack( hwnd: THandle; uMsg, idEvent, dwTime: Cardinal);
begin try Inc(NTimerCount); FormCallBack.AddToList( IntToStr(NTimerCount) at TimeToStr(Now)); except on E: Exception do Application.HandleException(nil); end;
end; try-except块在那里是为了避免任何异常被发送回Windows——这是您必须对回调或DLL函数一致遵守的规则。
12.1.5 类属性
使用静态类方法的原因之一是实现类属性。什么是类属性与标准属性一样类属性是一个与读写机制关联的符号。与标准属性不同的是类属性与类相关并且必须使用类数据或静态类方法来实现。TBase 类再次来自 ClassStatic 示例用这两种方式中定义了两个类属性
typeTBase classprivateclass varFMyName: string;publicclass function GetMyName: string; static;class procedure SetMyName(Value: string); static;class property MyName: string read GetMyName write SetMyName;class property DirectName: string read FMyName write FMyName;end;在这个例子中TBase 类有两个类属性一个是 MyName它通过类方法 GetMyName 和 SetMyName 进行读写另一个是 DirectName它直接读写类变量 FMyName。
12.1.6 带有实例计数器的类
类数据和类方法可用于保存类整体方面的信息。这种类型的信息的一个示例可以是该类创建的实例总数或当前存在的实例数。CountObj 示例演示了这种情景。该程序并不是非常有用因为它只关注一个特定的问题。目标对象有一个简单的类只存储一个数值
typeTCountedObj class(TObject)privateFValue: Integer;privateclass varFTotal: Integer;FCurrent: Integer;publicconstructor Create;destructor Destroy; override;property Value: Integer read FValue write FValue;publicclass function GetTotal: Integer;class function GetCurrent: Integer;end; 每次创建对象时程序都会递增两个计数器。每次销毁对象时当前计数器都会减少
constructor TCountedObj.Create(AOwner: TComponent);
begininherited Create;Inc(FTotal);Inc(FCurrent);
end;destructor TCountedObj.Destroy;
beginDec(FCurrent);inherited Destroy;
end; 可以在不引用特定对象的情况下访问类信息。实际上可能在某个时刻内存中没有对象
class function TCountedObj.GetTotal: Integer;
beginResult : FTotal;
end; 可以使用类似以下的代码显示当前状态
Label1.Text : TCountedObj.GetCurrent.ToString / TCountedObj.GetTotal.ToString; 在演示中这段代码在一个定时器中执行的它更新一个标签因此它不需要引用任何特定的对象实例也不是直接由任何手动操作触发的。示例中的按钮只是创建和释放一些对象或者将一些对象保留在内存中实际上该程序存在一些潜在的内存泄漏。