Delphi XE5教程6:单元的结构和语法


内容源自Delphi XE5 UPDATE 2官方帮助《Delphi Reference》,本人水平有限,欢迎各位高人修正相关错误!

也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可QQ:[email protected]

2  Unit Structure and Syntax



A unit consists of types (including classes), constants, variables, and routines (functions and procedures). Each unit is defined in its own source (.pas) file.



A unit file begins with a unit heading, which is followed by the interface keyword. Following the interface keyword, the uses clause specifies a list of unit dependencies. Next comes the implementation section, followed by the optional initialization, and finalization sections. A skeleton unit source file looks like this:

一个单元以单元头(unit heading)开始,后面是接口(interface)关键字。接口关键字的下面,用uses子句列表指定单元的依赖关系。接下来是实现(implementation)部分,其次是可选的初始化(initialization)、结束化(finalization)部分。一个单元的基本结构看起来这样:


unit Unit1;


uses // List of unit dependencies goes here...

// Interface section goes here


uses // List of unit dependencies goes here...

// Implementation of class methods, procedures, and functions goes here...


// Unit initialization code goes here...


// Unit finalization code goes here...




The unit must conclude with the reserved word end followed by a period.

单元必须以end 后跟一个句点结束(end.)。


1.1   The Unit Heading



The unit heading specifies the unit's name. It consists of the reserved word unit, followed by a valid identifier, followed by a semicolon. For applications developed using Embarcadero tools, the identifier must match the unit file name. Thus, the unit heading:

单元头指定单元的名称。它以关键字unit 开始,后面跟一个有效标识符(指定单元名),并以分号结束。使用Embarcadero工具创建的程序,标识符必须和单元文件名相同。所以,单元头:

unit MainForm;


would occur in a source file called MainForm.pas, and the file containing the compiled unit would be MainForm.dcu. Unit names must be unique within a project. Even if their unit files are in different directories, two units with the same name cannot be used in a single program.

必须出现在源文件MainForm.pas 中,编译后的单元文件将是MainForm.dcu。在一个工程中,单元名必须是独一无二的,两个同名的单元不能用在同一个程序中,即使它们的单元文件位于不同的路径下。


1.2   The Interface Section

2.2 接口(interface

The interface section of a unit begins with the reserved word interface and continues until the beginning of the implementation section. The interface section declares constants, types, variables, procedures, and functions that are available to clients. That is, to other units or programs that wish to use elements from this unit. These entities are called public because code in other units can access them as if they were declared in the unit itself.

单元的接口部分从关键字 interface 开始,直到实现implementation部分的开头。接口部分声明常量、类型、变量、过程和函数,所有这些对单元的客户(也就是引用此单元的程序或其它单元)是可用的。在接口部分声明的实体被称为‘公用’的,因为它们对客户来说,就像自己声明的一样。


The interface declaration of a procedure or function includes only the routine's signature. That is, the routine's name, parameters, and return type (for functions). The block containing executable code for the procedure or function follows in the implementation section. Thus procedure and function declarations in the interface section work like forward declarations.

在接口部分声明的过程或函数只是一个例程头,也就是说,只它包含例程的名称、参数和返回类型(函数)。它们的代码块(block)在实现部分implementation定义。所以,在接口部分声明过程和函数就像使用forward 指示字,虽然这里它并没有出现。


The interface declaration for a class must include declarations for all class members: fields, properties, procedures, and functions.

在接口部分声明一个类时,必须包含它的所有成员:fields, properties, procedures, 和 functions.


The interface section can include its own uses clause, which must appear immediately after the keyword interface.

接口部分可以包含自己的 uses 子句,它必须紧跟在关键字interface 之后。


1.3   The Implementation Section

2.3 实现部分

The implementation section of a unit begins with the reserved word implementation and continues until the beginning of the initialization section or, if there is no initialization section, until the end of the unit. The implementation section defines procedures and functions that are declared in the interface section. Within the implementation section, these procedures and functions may be defined and called in any order. You can omit parameter lists from public procedure and function headings when you define them in the implementation section; but if you include a parameter list, it must match the declaration in the interface section exactly.

单元的实现部分从关键字 implementation 开始,直到初始化部分的开头;或者,如果没有初始化部分的话,就直到单元的结束。实现部分定义接口部分声明的过程和函数,在这里,你能以任何顺序定义和调用它们。并且,你也可以省略过程和函数的参数列表,但如果包括它们的话,就必须和在接口部分的声明完全相同。


In addition to definitions of public procedures and functions, the implementation section can declare constants, types (including classes), variables, procedures, and functions that are private to the unit. That is, unlike the interface section, entities declared in the implementation section are inaccessible to other units.



The implementation section can include its own uses clause, which must appear immediately after the keyword implementation. The identifiers declared within units specified in the implementation section are only available for use within the implementation section itself. You cannot refer to such identifiers in the interface section.

实现部分可以包含自己的 uses 子句,它必须紧跟在关键字implementation 之后。



1.4   The Initialization Section

2.4 初始化部分


The initialization section is optional. It begins with the reserved word initialization and continues until the beginning of the finalization section or, if there is no finalization section, until the end of the unit. The initialization section contains statements that are executed, in the order in which they appear, on program start-up. So, for example, if you have defined data structures that need to be initialized, you can do this in the initialization section.

初始化部分是可选的。它从关键字 initialization 开始,直到结束化部分的开头;或者,如果没有结束化部分的话,就直到单元的结束。初始化部分所包含的命令,将在程序启动时按它们出现的顺序开始执行。举例来说,如果你定义了需要初始化的结构,你可以在初始化部分来完成。


For units in the interface uses list, the initialization sections of the units used by a client are executed in the order in which the units appear in the client's uses clause.

对于一个单元(称为客户)引用的各个单元,它们的初始化将按客户单元中uses 子句引用它们的顺序开始执行。(也就是说,uses 子句中列在前面的单元先初始化)


The older "begin ... end." syntax still functions. Basically, the reserved word "begin" can be used in place of initialization followed by zero or more execution statements. Code using the older "begin ... end." syntax cannot specify a finalization section. In this case, finalization is accomplished by providing a procedure to the ExitProc variable. This method is not recommended for code going forward, but you might see it used in older source code.

旧的“begin…end.”语法仍然是函数。基本上,保留字“begin” 后跟零个或多个执行语句可在初始化中代替使用。使用旧代码“begin…end.”语法不能指定一个结束化finalization部分。在这种情况下,结束化部分是通过向程序提供一个ExitProc变量来完成。这个方法以后不推荐用在代码中,但是你可能会在旧的源代码中看到它。


2.5 The Finalization Section

2.5 结束化部分


The finalization section is optional and can appear only in units that have an initialization section. The finalization section begins with the reserved word finalization and continues until the end of the unit. It contains statements that are executed when the main program terminates (unless the Halt procedure is used to terminate the program). Use the finalization section to free resources that are allocated in the initialization section.

结束化部分是可选的,并且只有当一个单元具有初始化部分时才能包含它。结束化部分从关键字finalization 开始,直到单元的结束。结束化部分所包含的命令,将在主程序结束时被执行。使用结束化部分来释放在初始化部分分配的资源。


Finalization sections are executed in the opposite order from initialization sections. For example, if your application initializes units A, B, and C, in that order, it will finalize them in the order C, B, and A.

结束化部分的执行顺序和初始化执行的顺序相反。例如,如果你的程序以 A、B、C 的顺序进行初始化,结束化时的顺序则是C、B、A。


Once a unit's initialization code starts to execute, the corresponding finalization section is guaranteed to execute when the application shuts down. The finalization section must therefore be able to handle incompletely initialized data, since, if a runtime error occurs, the initialization code might not execute completely.