热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

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

内容源自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.

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


推荐阅读
  • vue使用
    关键词: ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • Windows7 64位系统安装PLSQL Developer的步骤和注意事项
    本文介绍了在Windows7 64位系统上安装PLSQL Developer的步骤和注意事项。首先下载并安装PLSQL Developer,注意不要安装在默认目录下。然后下载Windows 32位的oracle instant client,并解压到指定路径。最后,按照自己的喜好对解压后的文件进行命名和压缩。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
author-avatar
phpyi
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有