TmyEvent = procedure of object;
TmyClass = class
private
Fage: Integer;
FonHundred: TmyEvent;
procedure SetAge(const Value: Integer);
public
procedure SetOnHundred1;
procedure SetOnhundred2;
constructor Create;
property Age: Integer read Fage write SetAge;
property OnHundred: TmyEvent read FonHundred write FonHundred;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
constructor TMyClass.Create;
begin
FonHundred := SetOnHundred1;
end;
procedure TmyClass.SetAge(const value: Integer);
begin
if (Value > 0) and (Value < 200) then
Fage := Value;
if Value = 100 then
OnHundred;
end;
procedure TmyClass.SetOnhundred2;
begin
ShowMessage('100啦!');
end;
procedure TmyClass.SetOnHundred1;
begin
ShowMessage('刚好100啦');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
TmyCls: TmyClass;
begin
TmyCls := TmyClass.Create;
TmyCls.Age := 100;
TmyCls.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
TmyCls: TmyClass;
begin
TmyCls := TmyClass.Create;
TmyCls.OnHundred := TmyCls.SetOnhundred2;
TmyCls.Age := 100;
TmyCls.Free;
end;
//事件类型就是一个指针,它指向一个过程
初探类的字段.属性.方法.事件
最新推荐文章于 2024-08-27 13:06:13 发布