Introduction to Delphi (2) Individuals think the good Delphi summary introduction, concise, worth seeing

zhaozj2021-02-17  57

In-depth Delphi (2)

Unit file / compiler condition identifier

By Machine

Contrasting the project document, the unit file is significantly more than something:

Unit unit1;

Interface

Uses Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs

TYPE TFORM1 = Class (TFORM) private {private declaration} public {public declaration} END;

Var Form1: TFORM1;

IMPLEMENTATION

{$ R * .dfm}

INITIALIZATION

{Initialization here}

Finalization

{Pin-decreasing here}

End.

First, Program is replaced with unit, then there are two keywords of interface and importation to divide the entire unit into two parts, namely interfaces, and implementation. The interface portion corresponds to the header file portion in C, and if a unit is Used, it is actually a portion that includes an interface. The function prototype can be defined in this section, but the specific implementation section of the function (including functions in the class) can only appear in the Implementation section. Similar to C, the interface portions and implementation portions can appear for global and local restrictions. The Uses of the interface portion must appear next to the interface keyword, the Uses of the implementation section must also follow the Implementation keyword, just like they are together. As for the implementation, in fact, if the project file is changed into the Implementation, it seems to become the implementation of the unit, in fact, because first, the project file cannot be used by Uses, so the project is always partial; In the implementation section, it can also appear as a project in the project ... End. Block. The part of the begin ... End. Block actually played the role of initialization, but this is mainly preserved for the previous PASCAL syntax, and Delphi recommends using new Initialization and Finalization to initialize and pin. In the implementation, Begin ... End. Block, the initialization block, and the pin solution block are not required, but the entire unit must end with END. Keyword Initialization marks the beginning of the initialization block, when a compiled project is running, the initialization block of each unit is first executed, as for the unit first initialized, depending on the order of Uses, first by Uses first It is initialized. The initialization block and begin ... End. Blocks cannot appear at the same time, so it is recommended to use the initialization block so that the function of the pin solution can be used. The pin can only appear along with the initialization, but the initialization block can appear separately. The pin solution is automatically performed before the end of the program, and the order in which the unit is in the order of the unit is opposite to the order of Uses. It should be noted that the initialization block may be aborted by a previously executed, but Delphi ensures that the code is executed, so the code in the pinch block must consider unfull initialization.

Then a detailed description of the USES statement. Summary As mentioned earlier, the USES can appear in three places. In the two places of the unit, the unit of Uses must be in the project's search path, while the Uses statement in the project file can directly specify the path of the file:

Uses form, main, extra in '../extra/extra.pas';

In the unit file, if another unit is referenced at the interface portion, the implementation portion can also call the unit, but it is confusing that the unit implements the part reference cannot be called in the interface portion. At the time of programming, you should try to reference other units in a unit's implementation section, reducing references to the interface part to avoid so-called "cyclic reference" issues. For example, unit A references unit B in the interface portion, and unit B references the unit C in the same method, and the final unit C also references the unit A in the interface portion, which occurs when the cycle reference is compiled. The solution is to put the global statement into a concentrated unit, and put the reference to the implementation part. Sometimes I will encounter a problem, if a variable, or constant, function, or data type, in short, the same name is simultaneous in two referenced units, then when the call is called, which one will be called? ? The answer is the last one in the unit list of the USES statement, not the one appeared first! This is to pay attention to. If you cannot be determined that the version will be called, you can also add the prefix of the "cell name journal number" in front of the call to force the Delphi to call the corresponding unit version. There is also a class of special commands for controlling the compiler in units and engineering. This command is included in a combination of braces or "*" and "*)", it looks like a comment, but starts with the "$" symbol. For example, $ R in the example is telling the compiler that you have to include some resource files to the project. One set of commands, $ define, $ undef, $ IFDEF, $ IFNDEF, $ ELSE, and $ ENDIF, look like the macro definition command in C, but this definition in Delphi only plays the role of the switch, not Define macro, that is, can define a switch name so that it is in an open state, and if this switch is not defined, it is considered to be a state, but it cannot be defined as a program code as a program code, and compiled It is an automatic code replacement macro, and Delphi does not provide such a macro function. If you don't know the compiler instruction, please continue to look down, otherwise you can jump directly to the next chapter. Ok, first, what is compiled? Compile is the source code you have written, translated into the command that the CPU can execute. Typically, the compiler compiles each unit file, and the connector is responsible for connecting the compiled cell file to an executable file (Exe file). The reason why it is done because of different operating systems, and its executable format is different, such as DOS, Windows, Linux these operating systems. But as long as the CPU is compatible model, you can use the same compilation file format because the translation process is the same. For example, the compilation file OBJ file, even in DOS, can still be connected to Windows EXE, the compiler is only interested in the CPU, and the format of the execution file to be output is not interested. I personally feel that if you want to learn, if you know more about it, learn more, you can more integrate, so sometimes it may be very far away. Also, you have to learn the same thing, patience is essential! So I don't understand not to discover, I suddenly understand after a period of time, I have tried this before. Ok, go back to Delphi's define problem. Beginners are easy to understand what the role of such definitions and constant / variable declarations is:

{$ Define debug}

Begin {$ ifdef debug} Writeln ('debug is on'); {$ ENDIF} end;

Then compare this example:

CONST Debug = True;

Begin if debug dam Writeln ('debug is on'); end; end; two examples run results are exactly the same. But in fact, the program is not the same. In the second example, first, the program is compiled, then run, the CPU first determines whether the value of DEBUG is not zero, if yes, continue to call WRITELN, nothing special. But the first example is different. When compiling, the compiler discovers the $ define debug, so it sets the identity internally, records the Debug definition, then continue the compilation process, then The compiler found $ IFDEF Debug, the compiler views your own internal identification table, discovers that debug is already defined, so it continues to compile the source code between $ IFDEF and $ ENDIF, otherwise, it will Ignore this source code, as if they don't exist at all! Therefore, the program compiled by the first example is exactly the same as the following example:

Begin Writeln ('Debug is on');

In this way, the source code is compiled, and the compiler has been judged, compiling the corresponding program, not the program, and the CPU makes judgments, call the corresponding code. In this way, the operational efficiency of the program is significantly improved, c is an example of using this characteristic, so the execution efficiency of C is relatively high, the compilation speed of C is very slow, because the compiler should maintain very large List (don't forgetting C's macro definition than the complexity in Delphi, and the C is on such a macro definition), and constantly search the list and modify the program source code according to the corresponding definition, and finally the CPU instruction can be performed. translation. In the future, how to use other languages ​​in Delphi, including assembly and c, etc., so that the event processing section of the program can be done with delphi, and the assembly or C can be used in the distance of the demand speed. The development speed can be greatly improved, while the program running efficiency can still be maintained at a high level.

With the Define instruction, you can do something else, such as the example to the upper, define a debug compiler condition identifier, and then insert some of the code just want to execute when debugging The code, such as the recorder, each calling a function, which is controlled by $ IFDEF, is very convenient, if the program is going to be released, do not want to compile those debugged code, just comment away Debug Definitions can be, without having to comment each debug code. In some cases, you cannot use Delphi debuggers, such as using Direct Draw, or debug a DLL used by multiple programs, you can only record the content you want to debug and record it. Way to debug, this is very annoying, if you want to maintain such a program, it is best to use Define, otherwise, the change process will only make you more annoying. The role of $ define can also continue to be discussed, and all programs have multiple code versions, you can use the convenient features of $ define. Ok, this chapter said here. Please continue to pay attention to the appearance of the next chapter.

转载请注明原文地址:https://www.9cbs.com/read-30387.html

New Post(0)