我所看到的程序,每个类的定义和实现多是在同一个unit文件中完成的。不知道一个类(或部件)是否可以分开写在多个文件中?比如:方法定义在 unit1中,而实现放在unit2中。//---unit1-------------------
unit Unit1;interfacetype
Tmyclass = class(TObject)
private
procedure Go;
emd;implementationend.
//-unit2--------------------unit Unit2;interfaceimplementation
procedure TmyClass.Go;
begin
//...
end;end.//---------------------不知道是否有啥办法分开写呢? 或者只能写在同一个文件中?
unit Unit1;interfacetype
Tmyclass = class(TObject)
private
procedure Go;
emd;implementationend.
//-unit2--------------------unit Unit2;interfaceimplementation
procedure TmyClass.Go;
begin
//...
end;end.//---------------------不知道是否有啥办法分开写呢? 或者只能写在同一个文件中?
在另外一个unit中继承, 我知道的就是这样!
声明和定义是分开的
需要用到编译指令
举个例子,将完整的一个类拆成三个文件:
申明文件
Unit hh;Interfaceuses
Classes,pubu,sysutils,dialogs,Windows,Controls;type
TProductType=Class;
{$I '1.pas'}//必须用到编译指令
ImplementationUses Graphics,Db, hell;{$I '2.pas'}
end.
----------
1.pas中只有声明
---------
TBt=class(TBxOrder)
private
....
public
procedure aa();
....
end;
---------
2.pas中只有定义
--------
procedure TBt.aa();
begin
...end;
用{$INCLUDE xx}指令将类定义加入到文件内。。
分别对应 包含声明和定义的文件,声明文件,定义文件
-----------------下面是自动生成的文件0.pas,略加修改------------------
Unit hh;Interfaceuses
Classes,pubu,sysutils,dialogs,Windows,Controls;type
TProductType=Class;
{$I '1.pas'}//必须用到编译指令
ImplementationUses Graphics,Db, hell;{$I '2.pas'}
end.--------------------下面是一个文件1.pas-----------
1.pas中只有声明
---------
TBt=class(TBxOrder)
private
....
public
procedure aa();
....
end;
--------------下面是另一个文件2.pas---------------------------
2.pas中只有定义
--------
procedure TBt.aa();
begin
...end;
Syntax {$I filename}
{$INCLUDE filename}
Scope Local
ResThe $I parameter directive instructs the compiler to include the named file in the compilation. In effect, the file is inserted in the compiled text right after the {$I filename} directive. The default extension for filename is .pas. If filename does not specify a directory path, then, in addition to searching for the file in the same directory as the current module, Delphi searches in the directories specified in the Search path input box on the Directories/Conditionals page of the Project|Options dialog box (or in the directories specified in a -I option on the DCC32 command line).To specify a filename that includes a space, surround the file name with single quotation s: {$I 'My file'}.
There is one restriction to the use of include files: An include file can't be specified in the middle of a statement part. In fact, all statements between the begin and end of a statement part must exist in the same source file.