Delphi7中一个有趣的现象,不知道其他版本又没有这个现象 ( 积分: 300 )

  • 主题发起人 主题发起人 tseug
  • 开始时间 开始时间
T

tseug

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, 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;

procedure Test(A1: Integer
A2: String);

implementation

{$R *.dfm}

procedure Test;
begin
ShowMessage(Format('%d %s', [A1, A2]));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Test(1, 'Hello, World!');
end;

end.
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, 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;

procedure Test(A1: Integer
A2: String);

implementation

{$R *.dfm}

procedure Test;
begin
ShowMessage(Format('%d %s', [A1, A2]));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Test(1, 'Hello, World!');
end;

end.
 
怎么了,我在Delphi 6 下没看到特别的地方。
 
你觉得怪在哪里?你的这样的写法本来就很怪呢,面向对象思想可不是这样子的,呵呵
 
是编译器太聪明了。哈哈。
procedure Test;
begin
ShowMessage(Format('%d %s', [A1, A2]));
end;

procedure TForm1.Button1Click;
begin
Test(1, 'Hello, World!'+(Sender as TButton).Name);
end;

end.
 
[:(]
看看interface部分的声明
procedure Test(A1: Integer
A2: String);
再看看implementation部分的实现
procedure Test;
begin
ShowMessage(Format('%d %s', [A1, A2]));
end;
如果没有上面的,你是不是会以为A1,A2是全局变量?
 
tseug 大叔,这很正常呀,C 里面也有类似的用法的 :-)
 
这是符合语法规则的,如果你重载这个方法就不能这么做了
其他版本当然行

procedure TForm1.Button1Click
//去掉参数,一样道理
begin
Test(1, 'Hello, World!');
end;
 
我知道C的用法,但是如果你把Procedure换成function 就不行了,呵呵
 
怎么不行?测试过了?
 
我弄错了,function也行
interface
function Test(A1: Integer
A2: String): Integer;
implementation
function Test;
begin
ShowMessage(Format('%d %s', [A1, A2]));
Result := 0;
end;
 
可以的,看看Windows.pas单元,大部分API函数的声明和实现都是这样的,应该和Delphi的版本没有什么关系的,以下是一个例子:
function CreateFile(lpFileName: PChar
dwDesiredAccess, dwShareMode: DWORD;
lpSecurityAttributes: PSecurityAttributes
dwCreationDisposition, dwFlagsAndAttributes: DWORD;
hTemplateFile: THandle): THandle
stdcall;

function CreateFile
external kernel32 name 'CreateFileA';
 
那这么看来,函数调用要以Interface里声明的为主了
 
竟然有此事?
 
300分阿,哈哈

这叫向前声明forward,这个关键字都看过吧,不是说以Interface声明为主,而是Interface部分默认就是forward了,向前声明过的方法在具体实现时可以省略参数部分,
但是,如果方法有重载的话,那么就一定要完整的补上了!
 
moshengren is right![8D]

The forward directive replaces the block, including local variable declarations and statements, in a procedure or function declaration. For example,

function Calculate(X, Y: Integer): Real
forward;

declares a function called Calculate. Somewhere after the forward declaration, the routine must be redeclared in a defining declaration that includes a block. The defining declaration for Calculate might look like this:

function Calculate;
... { declarations }
begin
... { statement block }
end;

Ordinarily, a defining declaration does not have to repeat the routine's parameter list or return type, but if it does repeat them, they must match those in the forward declaration exactly (except that default parameters can be omitted). If the forward declaration specifies an overloaded procedure or function (see Overloading procedures and functions), then the defining declaration must repeat the parameter list.

A forward declaration and its defining declaration must appear in the same type declaration section. That is, you can't add a new section (such as a var section or const section) between the forward declaration and the defining declaration. The defining declaration can be an
external or assembler declaration, but it cannot be another forward declaration.

The purpose of a forward declaration is to extend the scope of a procedure or function identifier to an earlier point in the source code. This allows other procedures and functions to call the forward-declared routine before it is actually defined. Besides letting you organize your code more flexibly, forward declarations are sometimes necessary for mutual recursions.

The forward directive has no effect in the interface section of a unit. Procedure and function headers in the interface section behave like
forward declarations and must have defining declarations in the implementation section.
A routine declared in the interface section is available from anywhere else in the unit and from any other unit or program that uses the unit where it is declared.
 
还以为什么事呢,原来就这事,tseug你们这种牛人才知啊,真是的,懂那么多汇编还不知这个[:D]
 
又学到知识了,好!
 
后退
顶部