内容源自Delphi XE5 UPDATE 2官方帮助《Delphi Reference》,本人水平有限,欢迎各位高人修正相关错误!
也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可QQ:34484690@qq.com
1 Program Structure and Syntax
1程序的结构和语法
A complete, executable Delphi application consists of multiple unit modules, all tied together by a single source code module called a project file. In traditional Pascal programming, all source code, including the main program, is stored in .pas files. Embarcadero tools use the file extension .dpr to designate the main program source module, while most other source code resides in unit files having the traditional .pas extension. To build a project, the compiler needs the project source file, and either a source file or a compiled unit file for each unit.
一个完整的,可执行的Delphi应用程序由多个单元模块构成。一个项目文件调用单个源代码文件并将他们捆绑在一起。每个单元保存在一个单独的文件中并分别进行编译,编译后的单元被链接到程序中。在传统的 Pascal 编程中,所有源代码,包括主程序都存储在.pas 文件中。Embarcadero工具使用一个工程文件(.dpr)来存储‘主’程序,而大部分源代码则保存在单元文件(.pas)中。要编译一个项目,编译器需要项目源文件,以及一个源文件或每个单元一个编译单元文件。
Note: Strictly speaking, you need not explicitly use any units in a project, but all programs automatically use the System unit and the SysInit unit.
注:严格来说,你不需要显式地使用任何一个项目单元,但所有程序自动使用System 单元和SysInit 单元。
The source code file for an executable Delphi application contains:
一个可执行的Delphi应用程序的源代码文件中包含:
a program heading,
一个程序头(program heading),
a uses clause (optional), and
一个 uses 子句(可选),和
a block of declarations and executable statements.
一个包含声明和命令语句的块(block)。
程序头指定程序的名称;uses 子句列出了程序引用的单元;块包含声明和命令语句。
The compiler, and hence the IDE, expect to find these three elements in a single project (.dpr) file.
当程序运行时,这些命令将被执行。IDE 期望在一个工程文件(.dpr)中找到以上三种元素。
1.1 The Program Heading
1.1 程序头
The program heading specifies a name for the executable program. It consists of the reserved word program, followed by a valid identifier, followed by a semicolon. For applications developed using Embarcadero tools, the identifier must match the project source file name.
程序头指定可执行程序的名称。它是程序的保留字,接着是一个有效的标识符,后面跟着一个分号。对于使用Embarcadero工具开发的程序,该标识符必须和项目源文件名匹配。
The following example shows the project source file for a program called Editor. Since the program is called Editor, this project file is called Editor.dpr.
下面的实例显示了一个叫做 Editor 的程序的项目源文件.它以关键字 program 开始,后面跟一个有效标志符(指定程序名),并以分号结束。标志符必须和工程文件名相同,在上例中,因为程序叫Editor,工程文件应该是EDITOR.dpr。
program Editor; uses Forms, REAbout, // An "About" box REMain; // Main form {$R *.res} begin Application.Title := 'Text Editor'; Application.CreateForm(TMainForm, MainForm); Application.Run; end.
The first line contains the program heading. The uses clause in this example specifies a dependency on three additional units: Forms, REAbout, and REMain. The $R compiler directive links the project's resource file into the program. Finally, the block of statements between the begin and end keywords are executed when the program runs. The project file, like all Delphi source files, ends with a period (not a semicolon).
第一行包含该程序的程序头。例子中的uses子句指定它与其它三个units的依赖关系:Forms, REAbout, 和 REMain。$R编译指令链接项目的资源文件到程序中。最后,当程序运行时会执行begin和end之间的语句块。和所有源文件一样,工程文件以一个.句点(不是分号)结束。
Delphi project files are usually short, since most of a program's logic resides in its unit files. A Delphi project file typically contains only enough code to launch the application's main window, and start the event processing loop. Project files are generated and maintained automatically by the IDE, and it is seldom necessary to edit them manually.
Delphi工程文件通常很短,因为绝大部分的程序逻辑位于单元文件中。一个Delphi项目文件通常只包含足够的代码以启动应用程序的主窗口,并启动事件处理循环。工程文件是由IDE自动产生并自动维护的,很少需要手工编辑。
In standard Pascal, a program heading can include parameters after the program name:
在标准Pascal中,程序头的程序名称后面包括参数:
program Calc(input, output);
Embarcadero's Delphi ignores these parameters.
Embarcadero的Delphi忽略这些参数。
In RAD Studio, the program heading introduces its own namespace, which is called the project default namespace.
在RAD Studio中,程序头引入自己的命名空间,这就是所谓的默认的命名空间。
1.2 The Program Uses Clause
1.2程序的uses 子句
The uses clause lists those units that are incorporated into the program. These units may in turn have uses clauses of their own. For more information on the uses clause within a unit source file, see Unit References and the Uses Clause, below.
uses 子句列出了共同构成程序的单元,这些单元可能包含自己的uses 子句。关于uses 子句,请参考单元引用和uses 子句。
The uses clause consists of the keyword uses, followed by a comma delimited list of units the project file directly depends on.
uses子句中包含关键字uses,其后的项目文件units列表用,进行分隔。
1.3 The Block
1.3 块
The block contains a simple or structured statement that is executed when the program runs. In most program files, the block consists of a compound statement bracketed between the reserved words begin and end, whose component statements are simply method calls to the project's Application object. Most projects have a global Application variable that holds an instance of Vcl.Forms.TApplication, Web.WebBroker.TWebApplication, or Vcl.SvcMgr.TServiceApplication. The block can also contain declarations of constants, types, variables, procedures, and functions; these declarations must precede the statement part of the block. Note that the end that represents the end of the program source must be followed by a period (.):
块包含一个简单语句或结构语句,程序运行时将执行它。在大多数程序中,块包含一个复合语句,它(复合语句)由关键字begin 和end 括起来,其中的命令只是简单调用Application 对象的方法。大多数工程都有一个全局的Application 变量,它是Vcl.Forms.TApplication, Web.WebBroker.TWebApplication, 或者 Vcl.SvcMgr.TServiceApplication的一个实例。块也可以包含常量、类型、变量、过程和函数的声明,它们必须位于(块中)命令语句的声明部分(前面)。需要注意的是,表示源程序结尾的end后必须跟一个句点(.):
begin . . . end.