如何在Delphi中使用Unit文件
我只是想掌握单独单元的窍门,以使我的代码更加封装。 我正在尝试整理我的方法的公共/私有声明,以便我可以从使用 testunit
的其他单元调用它们。 在此示例中,我希望将 hellofromotherunit
设为公共,但将 stickletters
设为私有。
unit testunit;
interface
uses
Windows, Messages, Dialogs;
implementation
function stickletters(a,b:string):string;
begin
result:=a+b;
end;
procedure hellofromotherunit();
begin
showmessage(stickletters('h','i'));
end;
end.
我似乎无法从其他单位复制私人/公共结构,如下所示:
Type
private
function stickletters(a,b:inter):integer;
public
procedure hellofromotherunit();
end
I'm just trying to get the hang of separate units to make my code more encapsulated.
I'm trying to get the public/private declarations of my methods sorted out, so I can call them from other units that use testunit
. In this example I want to make hellofromotherunit
public, but stickletters
private.
unit testunit;
interface
uses
Windows, Messages, Dialogs;
implementation
function stickletters(a,b:string):string;
begin
result:=a+b;
end;
procedure hellofromotherunit();
begin
showmessage(stickletters('h','i'));
end;
end.
I could not seem to copy the private/public structure from other units as in:
Type
private
function stickletters(a,b:inter):integer;
public
procedure hellofromotherunit();
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Unit 结构看起来有点像对象的公共/私有部分,你可以说它是它们的先驱。 但语法不同。
您只需在接口部分声明方法头,如下所示:
每个部分仅允许一个。
The Unit structure looks a bit like the public/private sections from objects, you could say it is their forerunner. But the syntax is different.
You only have to declare the method header in the interface section, as in:
Only one of each section allowed.
私人& 公共仅适用于班级。
您要做的是将 hellofromotherunit 声明的副本放入接口部分。 但是,请勿将粘性声明的副本放在那里。
界面部分中出现的任何内容实际上都是公开的。 仅在实现中的任何内容都是私有的。
Private & Public only apply to classes.
What you want to do is put a copy of the declaration of hellofromotherunit into the interface section. Do not put a copy of stickletter's declaration up there, however.
Anything that appears in the interface section is effectively public. Anything that's only down in the implementation is private.
此外,
每个单元都有两个不同的部分。 接口和实现。
接口部分包含所有公共定义(类型、过程标题、常量)。 实施部分包含所有实施细节。
当您使用一个单位时,(使用uses子句)您可以访问该单位的公共定义。 此访问不是递归的,因此如果单元 A 接口使用单元 B,并且单元 C 使用单元 A,则您无法访问单元 B,除非显式使用它。
实现部分可以访问接口以及两个 use 子句(接口和实现)中使用的单元。
首先编译所使用单元的接口,然后再继续编译其余单元。 这样做的好处是,您可以在实现中具有循环依赖关系:
编译:
每个单元还有一个初始化部分(如果它有一个初始化部分,它也可以有一个终结部分。)初始化部分用于初始化单元的变量。 最终确定部分用于清理。
当您使用这些单元时,明智的做法是不要依赖其他单元的初始化。 只要保持简单和简短即可。
单元也是命名空间。
请考虑以下情况:
如果在多个使用单元中定义了标识符,则采用使用列表中可能的最后一个单元。 所以f1 = 2。但是你可以在它前面加上单元(命名空间)名称的前缀来解决这个问题。
随着 .net 的引入,允许使用多部分命名空间,这会引入其他好问题:
在这种情况下,您会遇到冲突。 但这可以通过给予命名空间名称更高的优先级来解决。 所以第一行失败了。
In addition,
Each unit has two distinct parts. The interface and the implementation.
The interface section contains all public definitions (types, procedure headings, constants). The implementation section contains all implementation details.
When you use a unit, (using the uses clause) you get access to the public definitions of that unit. This access is not recursive, so if unit A interface uses unit B, and unit C uses unit A, you do not get access to unit B unless you use it explicitly.
The implementation section has access to the interface, to the unit used in both uses clauses (interface and implementation).
The interfaces of used units are compiled first before it continues compiling the rest. This has the advantage that you can have circular dependencies from within the implementation:
Which compiles:
Each unit also has an initialization section (and if it has an initialization section, it could also have a finalization section.) The initialization section is used to initialize the variables of the unit. The finalization sections are used to cleanup.
When you use these, its wise not to count on initializations of other units. Just keep them simple and short.
Unit also are namespaces.
Considder the following:
If an identifier is defined in multiple used units, the last unit possible in the uses list is taken. So f1 = 2. But you can prefix it with the unit (namespace) name to solve this problem.
With the introduction of .net, multi part namespaces are allowed which introduces other nice problems:
In this case you have a conflict. But that is resolved by giving namespace names higher priority. So the first line fails.
只需不在接口部分声明方法即可,它将保持私有。
Simply do not declare method in interface section and it will be kept private.