类:初识类的事件
先勾画一下思路:
1、建立一个类, 里面有年龄字段 FAge;
2、通过 Age 属性读写 FAge;
3、如果输入的年龄刚好是 100 岁, 将会激发一个事件, 这个事件我们给它命名为: OnHundred
Code
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//先定义一个特殊的类型,一个对象所属的过程类型,这就是建立一个事件的前提
TMyEvent = procedure of Object;
TMyClass = class
strict private
FAge: Integer; //年龄字段
FOnHundred: TMyEvent; //为我们刚刚定义的TMyEvent类型指定一个变量:FOnHundred
procedure SetAge(const Age: Integer);
public
procedure SetOnHundred1; //建立事件要调用的过程
procedure SetOnHundred2; //建立事件要调用的过程
constructor Create;
property Age: Integer read FAge write SetAge;
property OnHundred: TMyEvent read FOnHundred write FOnHundred; //其它事件也是属性,事件命名一般由On开始
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TMyClass }
constructor TMyClass.Create;
begin
FOnHundred := SetOnHundred1; //在对象建立时,我们先让事件调用 SetOnHundred1方法
end;
procedure TMyClass.SetAge(const Age: Integer);
begin
if (Age > 0) and (Age < 200) then FAge := Age;
if Age = 100 then OnHundred; //当输入的年龄是100岁时,激活事件
end;
procedure TMyClass.SetOnHundred1;
begin
ShowMessage('祝您100岁大寿!');
end;
procedure TMyClass.SetOnHundred2;
begin
ShowMessage('但愿我们都能活到100岁。');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MyClass: TMyClass;
begin
MyClass := TMyClass.Create;
MyClass.Age := 99; //如果年龄不是负数而又小于200
ShowMessage(IntToStr(MyClass.Age)); //99 对象就接受了
MyClass.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
MyClass: TMyClass;
begin
MyClass := TMyClass.Create;
MyClass.Age := 100;
MyClass.Free;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
MyClass: TMyClass;
begin
MyClass := TMyClass.Create;
MyClass.OnHundred := MyClass.SetOnHundred2; //让事件指定另一个方法
MyClass.Age := 100; //这时会激活事件,弹出对话框:但愿我们都能活到100岁
MyClass.Free;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//先定义一个特殊的类型,一个对象所属的过程类型,这就是建立一个事件的前提
TMyEvent = procedure of Object;
TMyClass = class
strict private
FAge: Integer; //年龄字段
FOnHundred: TMyEvent; //为我们刚刚定义的TMyEvent类型指定一个变量:FOnHundred
procedure SetAge(const Age: Integer);
public
procedure SetOnHundred1; //建立事件要调用的过程
procedure SetOnHundred2; //建立事件要调用的过程
constructor Create;
property Age: Integer read FAge write SetAge;
property OnHundred: TMyEvent read FOnHundred write FOnHundred; //其它事件也是属性,事件命名一般由On开始
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TMyClass }
constructor TMyClass.Create;
begin
FOnHundred := SetOnHundred1; //在对象建立时,我们先让事件调用 SetOnHundred1方法
end;
procedure TMyClass.SetAge(const Age: Integer);
begin
if (Age > 0) and (Age < 200) then FAge := Age;
if Age = 100 then OnHundred; //当输入的年龄是100岁时,激活事件
end;
procedure TMyClass.SetOnHundred1;
begin
ShowMessage('祝您100岁大寿!');
end;
procedure TMyClass.SetOnHundred2;
begin
ShowMessage('但愿我们都能活到100岁。');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MyClass: TMyClass;
begin
MyClass := TMyClass.Create;
MyClass.Age := 99; //如果年龄不是负数而又小于200
ShowMessage(IntToStr(MyClass.Age)); //99 对象就接受了
MyClass.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
MyClass: TMyClass;
begin
MyClass := TMyClass.Create;
MyClass.Age := 100;
MyClass.Free;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
MyClass: TMyClass;
begin
MyClass := TMyClass.Create;
MyClass.OnHundred := MyClass.SetOnHundred2; //让事件指定另一个方法
MyClass.Age := 100; //这时会激活事件,弹出对话框:但愿我们都能活到100岁
MyClass.Free;
end;
end.