!!!!!!!!!!!!关于声明的问题!!!!!!!!!!!!(5分)

3

3333W

Unregistered / Unconfirmed
GUEST, unregistred user!
关于声明的问题

unit Unit1;

interface <------问题所在地

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
<------问题所在地
implementation

{$R *.DFM}
==================================
我在书上看到,interface和var区域都是用来声明函数和过程等等东西的.
可是我做了试验,好像不要在var区域声明函数也可以编译完成,不知道是为什么.
如果不需要声明也能编译,那为什么书上还要这么说.我实在是不明白,那位高手可以
细细解说一下,还有就是,在interface区域声明函数,过程等等,和在var区域声明有什么不一样呢?
 
什么叫不需要声明也能编译呀?
 
>>interface和var区域都是用来声明函数和过程等等东西的.
非也。 var是用来声明变量的,而interface则是用来声明Delphi单元的接口的——如果一个
单元(A)被另一个单元(B)uses,那么在B中就可以“看到”A中interface部分的结构、常、变量
定义。

>>不要在var区域声明函数也可以编译完成
编译在 Application.CreateForm(TForm1, Form1)
一句出错,因为它找不到Form1。
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
Function Mysum(x,y:integer):integer;<----书上有这句(但是去掉好像也不报错)
implementation

{$R *.DFM}
Function Mysum(x,y:integer):integer;
begin
Result:=x*y;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
showmessage(inttostr(Mysum(8,9)));
end;

end.

如上:为什么去掉那一句,也没问题;.
 
谁来告诉我![:(][:(]
 
如果你没有在interface后面,implementation前面声明一个函数,
你这里是MySum,只在implementation中写了MySum函数的实现,
那么,只有写在MySum函数后面的其他函数才能调用MySum,而如果
你在interface中写了MySum的声明,那么,在implementation中
的任何函数都可以调用MySum了。你可以去掉var中的MySum声明,
然后把MySum的实现移到end.前面,再编译一下试试看。
 
多人接受答案了。
 
顶部