码农的笔记

Delphi虽好,但已不流行; 博客真好,可以做笔记

博客园 首页 新随笔 联系 订阅 管理

开发环境Delphi7

 

self报错问题

 

 

 1 unit Unit1;
 2 
 3 interface
 4 
 5 uses
 6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7   Dialogs, StdCtrls,SysClass_u, ExtCtrls;
 8 
 9 type
10  
11   
12   TForm1 = class(TForm)
13     Button1: TButton;
14     Button2: TButton;
15     Panel1: TPanel;
16     Button4: TButton;
17     procedure Button1Click(Sender: TObject);
18     procedure Button2Click(Sender: TObject);
19     procedure Button4Click(Sender: TObject);
20 
21   private
22     { Private declarations }
23   public
24     { Public declarations }
25   end;
26 
27 var
28   Form1: TForm1;
29 
30 procedure sssss(aObj,Sender: Tobject);
31 
32 implementation
33 
34 uses
35   Unit3;
36 
37 {$R *.dfm}
38 procedure sssss(aObj,Sender: Tobject);
39 begin
40   ShowMessage(TForm(aObj).name); //如果Button4点击过 ,再点击Button2,这句也会报错  //和Button1Click中注释的那句一样
41   ShowMessage(TForm(Sender).name);
42   ShowMessage('000');
43 end;
44 procedure TForm1.Button1Click(Sender: TObject);
45 var
46   s:Integer;
47 begin
48   //为了证明Self赋值问题,self其实是一个隐形参数
49   //ShowMessage(self.name);会报错
50   ShowMessage(IntToStr(Integer(@self))); //这个比下面的这个Integer(@Sender)大四个字节
51   ShowMessage(IntToStr(Integer(@Sender)));//这个比下面的这个Integer(@s)大四个字节
52   ShowMessage(IntToStr(Integer(@s)));
53   //由这几个地址充分证明了self是一个隐形参数
54   //学过汇编应该清楚栈相关的问题,栈的内存大小是定死的,是由程序编译时就定义好了,集成开发环境都有对栈的大小进行设置;
55   //栈是由高地址向低地址延伸的,比如当前栈顶地址为200208,压一个32位的Integer进去后,当前栈顶为200204
56   //不做过多解释
57   //仅做个笔记
58 
59   ShowMessage((Sender as TComponent ).name);
60 end;
61 
62 procedure TForm1.Button2Click(Sender: TObject);
63 var
64   s:TNotifyEvent;
65 begin
66   s:=Button1.onClick;
67   sss(@s);
68 end;
69 
70 procedure TForm1.Button4Click(Sender: TObject);
71 var
72   s:TNotifyEvent;
73 begin
74   @Button1.onClick:=@sssss;//把sssss赋值给Button1.onClick
75 end;
76 
77 end.

 

 

 1 unit Unit3;
 2 
 3 interface
 4 
 5 uses
 6   Classes,Forms;
 7   procedure sss(pp:Pointer);
 8 
 9 implementation
10 
11 uses
12   Unit2;
13 
14 procedure sss(pp:Pointer);
15 begin
16   Form2:=TForm2.Create(Application);
17   @Form2.OnMyProc:=pp;
18   Form2.Show;
19 end;
20 end.

 

 1 unit Unit2;
 2 
 3 interface
 4 
 5 uses
 6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7   Dialogs, StdCtrls,SysClass_u;
 8 
 9 type
10   TForm2 = class(TForm)
11     Button1: TButton;
12     procedure FormDestroy(Sender: TObject);
13     procedure Button1Click(Sender: TObject);
14   private
15     FOnMyProc:TNotifyEvent;
16     { Private declarations }
17   public
18     property OnMyProc :TNotifyEvent read FOnMyProc write FOnMyProc;
19     { Public declarations }
20   end;
21 
22 var
23   Form2: TForm2;
24 implementation
25 
26 {$R *.dfm}
27 
28 procedure TForm2.FormDestroy(Sender: TObject);
29 begin
30   OnMyProc:=nil;
31 end;
32 
33 procedure TForm2.Button1Click(Sender: TObject);
34 begin
35   OnMyProc(Sender );
36 end;
37 
38 end.

 

posted on 2021-12-14 17:10  码农的笔记  阅读(142)  评论(0编辑  收藏  举报