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

也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可QQ:34484690@qq.com

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.

一个单元由类型(包括类)、常量、变量以及例程(函数和过程)构成,每个单元由它自己的单元文件(.pas)定义。

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;

interface

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

  // Interface section goes here

implementation

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

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

initialization

// Unit initialization code goes here...

finalization

// Unit finalization code goes here...

end.

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

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

1.1   The Unit Heading

2.1单元头

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 之后。

实现部分内指定的units标识内仅可供实现部分自身使用,你不能把这样的部分放在接口interface部分。

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.

只要初始化部分的代码开始执行,在程序结束时相应的结束化部分就一定要执行。因此,结束化部分必须能够处理没有完全初始化的数据,因为,如果发生运行时错误,初始化部分的代码可能没有完全执行。

Delphi XE5教程6:单元的结构和语法的更多相关文章

  1. Delphi XE5教程5:程序的结构和语法

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...

  2. Delphi XE5教程7:单元引用和uses 子句

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...

  3. Delphi XE5教程4:程序和单元概述

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误!也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可 ...

  4. Delphi XE5教程1:语言概述

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...

  5. Delphi XE5教程11:Tokens

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误!也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可 ...

  6. Delphi XE5教程9:基本语法元素

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误!也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可 ...

  7. Delphi XE5教程8:使用Delphi命名空间

    // Project file declarations... //项目文件声明… program MyCompany.ProjectX.ProgramY; // Unit source file d ...

  8. Delphi XE5教程3:实例程序

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...

  9. Delphi XE5教程2:程序组织

    内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误! 也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者 ...

随机推荐

  1. shake震动动画

  2. iOS (catagroy)类别

    1:可以为类添加新的方法,但不能添加实例变量. 2:第一,无法向类中添加新的实例变量.类别没有位置容纳实例变量. 第二,名称冲突,即类别中的方法与现有的方法重名.当发生名称冲突时,类别具有更高的优先级 ...

  3. [Arduino] 基于Xbee Pro和网络技术的智能公交系统设计

    转自:http://www.21ic.com/app/rf/201112/99474.htm 引言 公共交通具有个体交通无法比拟的强大优势,优先发展城市公共交通系统是解决大.中城市交通问题的最佳途径. ...

  4. 【Android 界面效果25】android中include标签的使用

    在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过<include ...

  5. 【Mood-5】14条建议,使你的IT职业生涯更上一层楼

    升值为企业IT部门的领导者,并非一件易事.从一般大众中脱颖而出,则更不容易. 2013是一个好年头,据专家报告显示,6月所有新工作中,10%来自技术领域.这对于那些希望高升.换岗.跳槽的IT技术人员来 ...

  6. 整合iis+tomcat

    目的: 将 Tomcat与 IIS整合在一起,共用 80端口.让 iis可以解析 *.asp. *.aspx. *.jsp. servlet和 *.do文件: 第一步:准备工作. 在你的 Tomcat ...

  7. iOS - 第三方框架 - AFN

    #5.AFNetworking 2.6使用方法 >2.6版本 支持 iOS7以上,而且支持NSURLConnectionOperation >3.0版本 支持 iOS7以上 NSURLCo ...

  8. find 忽略文件夹选项-prune的说明

    注意:因为习惯在当前路径查找时候,常忽略./ 的指定,但读者不要因此而完全忘记find的格式. 查找时忽略指定目录,是要使用-prune选项,但实际上最重要的还是要和path配合.-prune的意义是 ...

  9. sqlserver跟踪

    本文以实际应用为目的,不在理论方面深究 1.打开跟踪器 或 2.新建跟踪-事件选择-列筛选器,HostName默认不显示,需勾选“显示所有列”,如果希望只跟踪某一客户端,可按下面的设置HostName ...

  10. sysobjects.xtype介绍

    SQL Server数据库的一切信息都保存在它的系统表格里. 在大多数情况下,对你最有用的两个列是Sysobjects.name和Sysobjects.xtype.前面一个用来列出待考察对象的名字,而 ...