Delphi basic tutorial graphic version of the unit file

After writing the opening article, consider what to write for a long time. Delphi uses unit as its program composition. The suffix of the unit file is .pas. If you want to think about it or start from the unit file

We can see the following description in the official document

  • Divide large programs into modules that can be edited separately.
  • Create libraries that can be shared between programs.
  • Distribute the library to other developers without providing the source code.

A complete, executable Delphi application consists of multiple unit modules, all of which are bundled together by a source code module called a project file . The extension of the project file is .dpr

Official document: http://docwiki.embarcadero.com/RADStudio/Sydney/en/Programs_and_Units_(Delphi)

Structure and syntax-.dpr

Create a new VCL Application manually . You can see the source code at the beginning of the program through the following figure

Insert picture description here

The code is as follows:

program Project1;

uses
  Vcl.Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

The source code files of the executable Delphi application include:

(1) program statement: the current project name is used by default

(2) uses statement declares: use clause is composed by the keyword use, followed by a comma-delimited project files directly dependent on the unit list

(3) Executable statement block

begin
  .
  .
  .
end.

The composition statement is just a method call to the Application object of the project. Most projects have a global Application variable that contains instances (objects) 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 of the program source must be followed by an English status period

It can be seen from the above that Delphi programs are indeed organized through .dpr files, where .pas is automatically introduced through the project creation wizard when creating the program. If you want to use the ordinary unit file manually created by yourself in the future, you still need to import it here.

Structure and syntax-.pas

Files ending with this suffix name are ordinary unit files, and this is also the most frequently used file type.

The creation method is also very simple. It can be created by **File–>New–>Unit ** in the top menu of the IDE, or by the method shown below

Insert picture description here

A unit is composed of types (including classes), constants, variables, and routines (functions and procedures).

The unit file starts with unit , followed by the interface keyword. After the interface keyword, the uses clause specifies a list of unit dependencies. The next step is to achieve partial implementation , then optional initialization and partial finalization . The unit source file is as follows:

unit Unit1;

interface

uses // 依赖清单,或者称为引入的其他单元清单,一般引入系统单元
  // 声明部分

implementation

uses // 依赖清单,或者称为引入的其他单元清单。一般引入自定义单元,可避免互相引用

// 类、方法、过程和函数的实现在这里...

initialization

//初始化部分
// 程序启动时先执行,并顺序执行。一个单元的初始化代码运行之前,就运行了它使用的每一个单元的初始化部分


finalization

// 结束化部分,程序结束时执行

end.

Interface section

(1) The interface part declares constants, types, variables, procedures and functions. These declarations are like their own declarations to the reference unit

(2) The procedures and functions declared in the interface part are like using the forward keyword (generally called forward declaration)

(3) The interface part can contain its own uses clause, which must appear immediately after the keyword interface.

Implementation section

(1) If it contains the words uses, it must be followed by the keyword implementation

(2) The procedures and functions defined in the interface are implemented here, which can be defined and called in any order.

(3) Private constants, types (including classes), variables, procedures, and functions of the unit can be defined, but these are invisible to customers who refer to the unit

Initialization section

Initialization is optional . It starts from the initialization of reserved words and continues until the beginning of finalization . If there is no finalization part, it continues until the end of the unit. The Initialization section contains statements that are executed in the order in which they appear when the program starts.

In other units, the initialization part of the unit will be executed in the order in which it appears in the uses statement.

Finalization section

The finalization part is optional and can only be used in units that have an Initialization part . The finalization part starts from the end of the reserved words and continues to the end of the unit. It contains statements that are executed when the main program terminates (unless the program is terminated using the Halt program). Use the finalization part to release the resources allocated in the Initialization part.

The execution order of the finalization part is opposite to that of the Initialization part. For example, if the application program initializes units A, B, and C in this order, it will complete their release in the order of C, B, and A.

Guess you like

Origin blog.csdn.net/farmer_city/article/details/109442935