Delphi Classes and Objects (5)-First Class Event

First sketch out the idea:
1. Create a class with an age field FAge;
2. Read and write FAge through the Age attribute;
3. If the entered age is exactly 100 years old, an event will be triggered. We named it as this event. : OnHundred


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);
   end ; {First define a special type: the type of procedure that an object belongs to; this is the premise of creating an event} 
  TMyEvent = procedure of object ; { TMyClass class) 
  TMyClass = class






    

    

  
  strict  private 
    FAge: Integer;            {age field} 
    FOnHundred: TMyEvent;     {specify a variable for the TMyEvent type we just defined: FOnHundred} 
    procedure SetAge ( const Value: Integer);
   public 
    procedure SetOnHundred1; { 
    procedure to be called by the establishment event} procedure SetOnHundred2; {Procedure to be called when establishing an event} 
    constructor Create;
     property Age: Integer read FAge write SetAge;
     property OnHundred: TMyEvent read FOnHundred write FOnHundred; {In fact, events are also properties} 
    {Event naming generally starts with On} 
  end; var 
  Form1: TForm1; implementation {$ R * .dfm} {TMyClass} constructor TMyClass.Create;
 begin 
  FOnHundred: = SetOnHundred1; {When the object is created, we first let the event call the SetOnHundred1 method} end ; procedure TMyClass.SetAge ( const Value: Integer);
 begin if (Value> 0 ) and (Value < 200 ) then 
    Fage: = Value; if Value = 100 then 
    OnHundred;       {When the input age is 100 years old, activate the event} end ; procedure TMyClass.SetOnHundred1 ;















  

   


begin 
  ShowMessage ( 'Congratulations on your 100-year-old birthday!' );
 end ; procedure TMyClass.SetOnHundred2;
 begin 
  ShowMessage ( 'I hope we can all live to be 100 years old!' );
 end ; // Test 1: procedure TForm1.Button1Click (Sender : TObject);
 var 
  myClass: TMyClass; begin 
  myClass: = TMyClass.Create;
  myClass.Age: = 99 ;                   {if the age is not negative but less than 200} 
  ShowMessage (IntToStr (myClass.Age)); {99; the object accepts } 
  MyClass.Free; end ; // Test 2: procedure TForm1.Button2Click (Sender: TObject);
 var












  myClass: TMyClass; begin 
  myClass: = TMyClass.Create; 
  myClass.Age: = 100 ; {The event will be activated at this time, a dialog box will pop up: Congratulations on your 100-year-old birthday!} 
  myClass.Free; end ; // Test 3: procedure TForm1.Button3Click (Sender: TObject);
 var 
  myClass: TMyClass; begin 
  myClass: = TMyClass.Create; 
  myClass.OnHundred: = myClass.SetOnHundred2; {let the event point to another method} 
  myClass.Age: = 100 ; {this time will The event will be activated, and a dialog box will pop up: I hope we can all live to be 100 years old!} 
  MyClass.Free; end ; {In 
  this section, I customized an event type: TMyEvent, which has no parameters, and 
  the event types defined by the general system are all There are parameters; the 
  event type is a pointer, it points to a process,










  The type and number of parameters are specified when the event type is defined. The parameters of
  The parameters of the process of event calling must be the same 
  as the definition. For example, TMyEvent has no parameters, and SetOnHundred1 and SetOnHundred2 it calls have no parameters. 
} 
End . 
Reprinted: https://www.cnblogs.com/del/archive/2008/01 /11/1034525.html

Guess you like

Origin www.cnblogs.com/fansizhe/p/12723798.html