The unit file is a Pascal source file with a . pas extension.
There are three types of cell files:
- Form/data modules and Frame unit files (Form/data module and frame units), typically generated automatically by Delphi.
- component's Unit file (component units), generated by you or Delphi.
- Common Unit files (general-purpose units), which have you created.
Let's take a look at the basic Pascal unit file structure.
Step1, on the main menu, selectFile | New | unit ", Delphi will create a new unit and display it in Code Editor.
Step2, let's take a look at one of the simplest Pascal units, including four keywords unit,interface,implementation , and end ;
Compare the whole unit as follows, please see the note:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
unit
Unit1;
interface
uses { List of units goes here }
Windows, Messages,
SysUtils, Variants,
Classes, Graphics,
Controls, Forms, Dialogs;
{ Interface section goes here }
type { type关键字用来声明一个新的类型}
TMyArray =
array
[
0..19
]
of
Byte
;
{ 声明TMyArray用来代替array[0..19] of Byte}
const
{ const关键字用来声明常量}
AppCation =
‘Hello World‘
;
{ AppCation由于在interface段声明,它在单元的任何地方都可以用}
var
{ var关键字用来声明变量,也分interface段和implementation段}
X:
Integer
;
MyArry: TMyArray;
{ MyArray为刚才定义的TMyArray新类型}
procedure
DoSomething;
{ 声明一个DoSomething过程}
implementation
uses
{ List of units goes here }
SysUtils, Variants;
var
ObjList: TObjectList;
const
{ BaseX,BaseY由于在implementation段声明,只能在单元内使用}
BaseX =
20
;
BaseY =
200
;
{ 实现interface段声明的DoSomething过程}
procedure
DoSomething;
begin
{ Code for DoSomething goes here.}
end
;
//C++风格的注释,只能用于单行注释
(*
相同类型注释不能嵌套
*)
{
推荐使用的注释符号
}
{ Implementation section goes here }
initialization
{ Initialization section goes here }
ObjList := TObjectList
.
Create;
finalization
{ Finalization section goes here }
FreeAndNil(ObjList);
end
.
|
Uses cell reference
The list of external cells referenced by a cell, where each cell must be separated by commas, and the last cell must be semicolon-delimited to indicate the end of the uses list.
Interface interface Segment
The output identifier for this unit of life, which is an item that can be accessed by other cells. The interface segment begins with interface and ends with implementation.
Implementation execution Segment
The execution segment starts with implementation, the following keyword ends, and the next keyword is usually the last keyword end of the cell. But in a unit with initialization, the next key is the initialization keyword.
The above three parts are required by the Unit unit. The next two keyword sections are optional.
Initialization unit initialization and end of Finalizaiton unit
Code used to perform startup and cleanup, any code in the initialization is executed when its unit is loaded into memory, and any code in the end segment is executed before the unit is cleaned out of memory.
You can have only one initialization segment, but not only the end segment, not the initialization segment.
Delphi Project composition of the unit file Pas