一个类(部件)是否可以写在多个文件中

xiaobd 2003-12-17 05:35:39
我所看到的程序,每个类的定义和实现多是在同一个unit文件中完成的。

不知道一个类(或部件)是否可以分开写在多个文件中?

比如:方法定义在 unit1中,而实现放在unit2中。

//---unit1-------------------
unit Unit1;

interface

type
Tmyclass = class(TObject)
private
procedure Go;
emd;

implementation

end.


//-unit2--------------------

unit Unit2;

interface

implementation


procedure TmyClass.Go;
begin
//...
end;

end.

//---------------------

不知道是否有啥办法分开写呢? 或者只能写在同一个文件中?

...全文
60 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
angelface 2003-12-18
  • 打赏
  • 举报
回复
学习~~~~
qiume 2003-12-18
  • 打赏
  • 举报
回复
// Include file

Type Parameter
Syntax {$I filename}
{$INCLUDE filename}
Scope Local
Remarks

The $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 marks: {$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.
fengsx 2003-12-18
  • 打赏
  • 举报
回复
!
cll007 2003-12-17
  • 打赏
  • 举报
回复
没有呀,一共三个文件呀0.pas,1.pas,2.pas
分别对应 包含声明和定义的文件,声明文件,定义文件
-----------------下面是自动生成的文件0.pas,略加修改------------------
Unit hh;

Interface

uses
Classes,pubu,sysutils,dialogs,Windows,Controls;

type
TProductType=Class;
{$I '1.pas'}//必须用到编译指令
Implementation

Uses 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;
tonylk 2003-12-17
  • 打赏
  • 举报
回复
最赖的一个办法:
用{$INCLUDE xx}指令将类定义加入到文件内。。
WuHeHai 2003-12-17
  • 打赏
  • 举报
回复
楼上这种方式的确可以达到文件分离。但最终还是把代码放在一个文件中了。没有达到楼主的要求。
cll007 2003-12-17
  • 打赏
  • 举报
回复
可以的
声明和定义是分开的
需要用到编译指令
举个例子,将完整的一个类拆成三个文件:
申明文件
Unit hh;

Interface

uses
Classes,pubu,sysutils,dialogs,Windows,Controls;

type
TProductType=Class;
{$I '1.pas'}//必须用到编译指令
Implementation

Uses Graphics,Db, hell;

{$I '2.pas'}
end.
----------
1.pas中只有声明
---------
TBt=class(TBxOrder)
private
....
public
procedure aa();
....
end;
---------
2.pas中只有定义
--------
procedure TBt.aa();
begin
...

end;

WuHeHai 2003-12-17
  • 打赏
  • 举报
回复
不可以。想都不用想。
如果可以分开写,你怎么知道这两个类之间是什么关系,如何来确定他们之间的关系。
也许仅仅是同名的两个类而已。
nyf1220 2003-12-17
  • 打赏
  • 举报
回复
分开写有什么意义吗?
增加代码的复杂性?
xiaobd 2003-12-17
  • 打赏
  • 举报
回复
不是 抽象方法 ,就是直接分开写。

不知道行不行啊。
Randy_Mic 2003-12-17
  • 打赏
  • 举报
回复
好像不能实现楼主说的;
aiirii 2003-12-17
  • 打赏
  • 举报
回复
将类的方法声明为抽象方法;
在另外一个unit中继承, 我知道的就是这样!

16,746

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧