16,748
社区成员
发帖
与我相关
我的任务
分享
TMyEvent = procedure of object;
{TMyClass 类}
TMyClass = class
strict private
FAge: Integer; {年龄字段}
FOnHundred: TMyEvent; {为我们刚刚定义的 TMyEvent 类型指定一个变量: FOnHundred}
procedure SetAge(const Value: Integer);
public
procedure SetOnHundred1; {建立事件将要调用的过程}
constructor Create;
property Age: Integer read FAge write SetAge;
property OnHundred: TMyEvent read FOnHundred write FOnHundred; {其实事件也是属性}
{事件命名一般用 On 开始}
end;
constructor TMyClass.Create;
begin
FOnHundred := SetOnHundred1; {在对象建立时, 我们先让事件调用 SetOnHundred1 方法}
end;
procedure TMyClass.SetAge(const Value: Integer);
begin
if (Value>0) and (Value<200) then
Fage := Value;
if Value=100 then
OnHundred; {当输入的年龄是 100 岁时, 激活事件}
end;
procedure TMyClass.SetOnHundred1;
begin
ShowMessage('xxxxxxxxxxxxxx!');
end;