拾
拾荒者
Unregistered / Unconfirmed
GUEST, unregistred user!
明白了! with 无非起了范围搜索优先作用。
如 with TestA,TestB do
变量或成员的搜索优先级别会是:
先搜 TestA,如果没有,再是 TestB ,最后才是默认例程。 :) (吐了一口气。)
原问题:
这是《DELPHI 深度历险》的例:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
type
// ﹚竡???癟?挡篶
TWowMessage = record
Msg: Cardinal;
WowStr: string;
end;
TDog = class
private
// Μ?癟? 100 ?, 穦㊣??よ猭
procedure Wow(var message: TWowMessage)
message 100;
end;
{ TDog }
procedure TDog.Wow(var message: TWowMessage);
begin
// ?沮癟?挡篶?戈?, ??羘
ShowMessage(message.WowStr);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Msg: TWowMessage;
begin
[h1][red]with TDog.Create do[/red][/h1]
try
// ??﹚癟?挡篶ず甧
Msg.Msg := 100;
Msg.WowStr := 'Ouch !!';
// だ??癟?
Dispatch(Msg);
finally
Free;
end;
end;
end.
我不明白的就是为何书中这段代码可编译通过。好像与 Object Pascal 语法书中所说
不相符呀?
按我理解应该
with TDog.Create do
try
Msg.Msg := 100;
Msg.WowStr := 'Ouch !!';
Dispatch(Msg);
finally
Free;
end;
end;
也应该等同于
var
Dog: TDog;
begin
Dog := TDog.Create;
with Dog do
try
Msg.Msg := 100;
Msg.WowStr := 'Ouch !!';
Dispatch(Msg);
finally
Free;
end;
end;
吧? 那么为何 Dog. 却能不冠于
Msg.Msg := 100
Msg.WowStr := 'Ouch !!';
之前呢?按理应等同 Dog.Msg.Msg := 100
呀?
如 with TestA,TestB do
变量或成员的搜索优先级别会是:
先搜 TestA,如果没有,再是 TestB ,最后才是默认例程。 :) (吐了一口气。)
原问题:
这是《DELPHI 深度历险》的例:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
type
// ﹚竡???癟?挡篶
TWowMessage = record
Msg: Cardinal;
WowStr: string;
end;
TDog = class
private
// Μ?癟? 100 ?, 穦㊣??よ猭
procedure Wow(var message: TWowMessage)
message 100;
end;
{ TDog }
procedure TDog.Wow(var message: TWowMessage);
begin
// ?沮癟?挡篶?戈?, ??羘
ShowMessage(message.WowStr);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Msg: TWowMessage;
begin
[h1][red]with TDog.Create do[/red][/h1]
try
// ??﹚癟?挡篶ず甧
Msg.Msg := 100;
Msg.WowStr := 'Ouch !!';
// だ??癟?
Dispatch(Msg);
finally
Free;
end;
end;
end.
我不明白的就是为何书中这段代码可编译通过。好像与 Object Pascal 语法书中所说
不相符呀?
按我理解应该
with TDog.Create do
try
Msg.Msg := 100;
Msg.WowStr := 'Ouch !!';
Dispatch(Msg);
finally
Free;
end;
end;
也应该等同于
var
Dog: TDog;
begin
Dog := TDog.Create;
with Dog do
try
Msg.Msg := 100;
Msg.WowStr := 'Ouch !!';
Dispatch(Msg);
finally
Free;
end;
end;
吧? 那么为何 Dog. 却能不冠于
Msg.Msg := 100
Msg.WowStr := 'Ouch !!';
之前呢?按理应等同 Dog.Msg.Msg := 100
呀?