Delphi exploration

xiaoxiao2021-03-06  56

1: Process type

⒈⒈ What is the process type

State a variable as a process type, you can assign a process or function as a whole to this variable or use this variable as

The parameters are passed to other processes or functions.

Declare a process type syntax similar to declaring a process or function of the first syntax, different is to reserve the word procedure or FUN-

Cartion does not require an identifier of a process or function, for example:

Type

Proc1 = procedure;

Proc2 = procedure (varX, y: integer);

Proc3 = function (x: double): double;

In the above example, the three process types were declared, the first is the process without any parameters, the second is the process of bringing two parameters, the third is

With a Double type parameter and return the Double type value.

Like a process or function, you can specify a call agreement when declaring a process type, default is Register

the way.

Below we will explain the usage of process types:

Type

Swapproc = procedure (var x, y: integer);

Mathfunc = function (x: double): double;

{Or above is the type declaration part}

VAR

P: swapproc;

F: mathfunc;

{The P designed with the PAPPROC type variable to describe the variable of the mathfunc type}

Procedure Swap (VAR A, B: Integer);

VAR

Temp: integer;

Begin

TEMP: = a;

A: = B;

B: = TEMP;

END;

Function TAN (Angle: Double);

Begin

Tan: = sin (Angle) / cos (Angle);

END;

{The above defines a process and a function}

P: = swap;

F: = TAN;

{The SWAP process is assigned to the P variable, and the TAN function is assigned to the F variable}

P (i, j); {equivalent to calling SWAP (i, j)}

X: = f (x); {equivalent to x: = tan (x)}

This example illustrates a process or function to assign a variable of a process type. In fact, the data of the process type can also be used as a para.

The number passed to other processes or functions.

Note: You cannot direct the process type as the return type of the function, but you can make the return type of the function to the pointer type, so that the pointer

Point to a process or function address.

When you assign a process or function to a variable of a process type, pay attention to assignment, only the following conditions are meticulous.

The call is the same must be the same.

The number of parameters must be the same, and the corresponding data type must be the same, but the parameter name is not necessarily the same.

The return type of the function must be the same.

Object Pascal specifies that NIL is compatible with any process type.

The process type is divided into two categories according to whether it is used for objects: a class is a global process pointer, and the other is the method pointer.

The global process pointer points to a global process or function, such as SwapproC and Mathfunc in the above example, such process types

There is no Of Object section.

The method pointer must be added to Object when it is, indicating that it is a method of an object, and the program is as follows:

Type

TNOTIFYEVENT = Procedure (Sender: TOBJECT) OF Object

With the method of the method, you must expand an object. You don't need to repart to a new one. Justify the other object to give this.

Method pointer is OK, which avoids too much level of the object.

Method pointer is usually used to associate an event of an object (possibly other objects) of the event (possibly other objects), for example, you want to define

The user clicks the default behavior when you are, you don't have to give a new object from the TButton object and overload its response clicks.

You only need to link a existing code to the same method pointer.

Examples of the method pointer are as follows:

Type

TNOTIFYEVENT = Procedure (Sender: TOBJECT) OF Object

Type

TanObject = Class (TOBJECT)

FONCLICK: TNOTIFYEVENT;

END;

TanotherObject = Class (TOBJECT)

Procedure amethod (sender: TOBJECT);

END;

VAR

ANOBJECT: TANOBJECT;

AnotherObject: TanotherObject;

Begin

Anobject: = TanObject.create;

AnotherObject: = TanotherObject.create;

Anobject.fonclick: = anotherObject.amethod;

END;

2: Type compatibility and assignment

⒈ type compatible

The above details of the data type in the Object Pascal language is introduced. Here, it is to point out that Object Pascal is a type.

Strict languages, not all types of data can be assigned to each other. For example, a C language programmer is accustomed to putting a whole

Value assigns a character type variable or assigns a real number to a integer variable, which is allowed in the C language, and

Object Pascal, the compiler will report an error. So, you don't have the difference in data types to be fully assigned to each other? Nor,

If an integer is assigned to a real variable, it is allowed because the data accuracy is not lost during the assignment process. The general principle is that only

The type of assignment number is assignable to assign a value.

Type compatibility

Before discussing assignment, let's introduce the concept of type compatibility, because type compatibility is a prerequisite for assignment, only type

The capacity variable can be confirmed.

What is easier to understand is that when the type of the two is exactly consistent, the value can always be performed, how is the problem is completely consistent, habits

Programmers used to C language must pay attention to the types in Object Pascal, have their own special provisions.

When satisfying one of the following two conditions, the type T1 and type T2 can be considered consistent:

T1 and T2 have exact same type identifiers.

T1 is declared as a type of T2 equivalent.

For example, the three types below are consistent:

T1 = integer;

T2 = integer;

T3 = T1;

The T1 and T2 types are consistent because they have exactly the same type identifier, T3 and T1 types, because T3 is declared and T1 equivalent

type.

Note: The following two types are not consistent, although their type description is exactly the same:

T1: Array [1..100] OF INTEGER;

T2: Array [1..100] OF INTEGER;

To make T2 become the type consistent with T1, it should be written:

T1, T2: Array [1..100] OF INTEGER;

Below we discuss the type of compatible concept, Object Pascal stipulates that satisfying one of the following conditions is considered to be type compatible:

Two types are consistent types.

Both types are real.

Both types are integer.

One type is another type of suborder.

Both types are sub-boundaries of the same host type.

Both types are collective set types of base types.

Both types are compact string types, and their elements must be the same.

One type is a string type, and the other can be a non-compact or compact string type or a character type. One type is the type of pointer and the other is the type of other pointer.

Both types are class types or class reference types, and one of them is another type of successor.

One type is PCHAR type, and the other type is zero-based character array array [0..n] of char.

Two types are pointers to the same type of data (compilation switch $ T is in ($ T-) state).

Both types are the type of process, the number of parameters, the parameter types are consistent, and if the function is also required to return the type.

One type is a Variant type, and the other type is an integer or real or string type or Boolean.

⒉ assignment

Meeting type compatibility can only be confirmed, only the assignment variable is the passage that can assign or perform parameter values.

Settings T1 and T2 are two types, to assume the T2 type value to the T1 type variable, Object Pascal regulates one of the following conditions.

It can be considered to be assignment:

T1 is an object type, T2 is its ancestral object type.

T1 and T2 are consistent types, but cannot be a file type or a structure type containing file type elements.

T1 and T2 are compatible ordered types, and T2's value domain is completely surrounded in the value domain of T1.

T1 and T2 are both real, and T2's value field is completely surrounded in the value domain of T1.

T1 is real, T2 is integer.

T1 and T2 are all string types.

T1 is a string type, T2 is a character type.

T1 is a string type, T2 is a compact string type.

T1 is a long string type, T2 is a PCHAR type.

T1 and T2 are compatible compact string types.

T1 and T2 are compatible set types, and T2's value field is completely surrounded in the value domain of T1.

T1 and T2 are compatible pointer types.

T1 is a class type, T2 is a class type inherited from T1.

T1 is the type of cost type, T2 is a class reference type inherited from T1.

T1 is a PCHAR type, T2 is a string constant.

T1 is the PCHAR type, T2 is zero-based character array array [0..n] of char.

T1 and T2 are compatible process types.

T1 is the process type, T2 is a process or function, and the number of parameters of both, the parameter types are consistent, if it is a function,

Require returns the same type.

T1 is a Variant type, T2 is an integer or real or string type or Boolean.

T1 is integer or real or string or Boolean, T2 is a Variant type.

The assignment is a prerequisite for assigning or participation. When the parameter is a variable parameter (see the variable for the concept of variable parameters).

The requirements must be strict, and it requires the type of ginseng and the arguments must be consistent.

Three; type forced conversion

⒈ Value type forced conversion

You can force a type of value to convert to another type, the syntax is as follows:

Type identifier (expression)

Type of values ​​Force transformation First specify a type identifier, then enclose the expression () to be converted, for example:

Integer ('a')

Char (48)

In the above example, the character A is converted into an integer, and an integer 48 is converted into a character.

Note: Object Pascal stipulates that the expression in the type identifier and the brackets is either an ordered type, or it is a pointer type.

Type of ⒉ Variables forced conversion

You can also convert a type of variable to another type of variable, including the variable of the process type, the syntax is as follows

Type identifier (variable reference)

Type of variables Forced conversion is for variable references, Object Pascal stipulates that the length of variable type to be converted must be

The length of the standard type is consistent, and it will cause an exception if it is inconsistent.

If the type forced conversion is performed by the AS operator, the AS operation can perform an inspection that is compatible before the conversion. If it is pass

If the type identifier is forced to convert, it is not checked.

The program is now as follows:

TYPE FUNC = Function (x: integer): integer;

VAR

F: func;

P: POINTER;

F: = func (p);

{Convert the variable P of the pointer to the FUNC type variable f}

FUNC (P): = F;

{First convert the variable p of the pointer to the FUNC type, then assign a FUNC type value to the converted variable P. }

4: Variable

⒈ variable statement

Types and variables are two concepts that have both differences and links. In general, the type cannot participate directly (later,

Types can be used directly without declaring an object instance, see class type description), you must declare a specified type

This type of data can be operated in order after variables.

In Object Pascal, the syntax of the declared variable is as follows:

VAR

Variable 1: Type;

Variable 2: Type;

...

Variable N: Type;

Variables are declared by reserving word Var, and the VAR can declare multiple variables at the same time, and if some variables are the same,

Together with the combination, separated from each other.

The declaration variable should pay attention to two points. First, the variable name is to comply with the rules of the Object Pascal language about identifiers.

The type of amount, the type is either Object Pascal language predefined standard type, or it is the previously declared custom type.

The program is now as follows:

TYPE DIGITS = set of 0..9;

VAR X, Y, Z: Integer;: {x, y, z type, combined with them together}

M, N: Digits; {DIGITS type is already in front declaration}

Object Pascal allows the number of the types of variables to be in one, which can simplify the program, such as in the above example, the variables M and N can be

This statement:

VAR M, N: SET OF 0..9;

It can also be seen from the grammatical diagram, and the variable can also be assigned a type constant when the variable is declared.

⒉ABSOLUTE clause

When declaring variables, you can also bring an optional absolute clause for specifying the absolute position of the value of the variable in memory. Absolute

The syntax of the clause is as follows:

Absolute unsigned integer: unsigned integer

This indicator can follow a segment value and an offset, such as:

Var Crtmode: Byte Absolute $ 0040: $ 0049;

In the above example, a Byte type variable CRTMode is declared. The first constant after the word is the segment value, and the second constant is the offset.

Segments and offsets can only be between $ 0000 and $ ffff.

Note: This form cannot be used in Windows protection mode because Windows does not allow your application to access the programmed memory area.

Direct write memory addresses can cause a GPF error, in fact, in Windows, generally declare a another variable, then in this

The storage address of the value of the specified variable in the sentence is the same as that variable, for example:

Var str: String;

Strlen: BYTE ABSOLUTE STR; in the upper example, the string type variable Str is declared, and then declare a BYTE type of variable strlen, the absolute clause specifies the variable

Strlen's address is the same as the address of the variable STR.

⒊ Global variables and local variables

The global variable is divided into two class. First, the public variables that can be accessed throughout the program must be declared in the Interface section of the unit, and the other is only

Limited to a common variable accessed by a unit, must be declared in the IMPLEMentation section of the unit, for global variables, you can declare

At the same time, an initial, for example:

Var x: integer = 1;

No explicit assignment, the initial value of this global variable is 0.

The role of local variables is limited to the block where the declaration is located, which is usually internally declared in the process, function or class, for example:

Procedure myproc:

Var x, y, z: integer;

Begin

...

End

In the above example, X, Y, Z is only defined in the process myproc.

For local variables, they cannot be assigned at the time of declaration, and their values ​​are uncertain before they are assigned to them.

Five: Type constant

⒈ Simple type constant

Simple type constants are type constants for the simple type (ordered type, and real), for example:

Const

Maximum: integer = 9999;

Factor: real = -0.1;

Breakchar: char = # 3;

In the above example, a constant constant Maximum, a real constant Factor, a constant confession of a control character type Breakchar.

⒉ ⒉ ⒉ type constant

The constant of the pointer type is usually declared with "constant address expression", and for the type constant of the PCHAR type, you can assign a value with a string.

The constant example of the type of pointer is as follows:

Type

TDIRECTION = (Left, Right, Up, Down);

Pnode = ^ node;

TNODE = Record

Next: PNODE;

Symbol: String;

Value: TDIRECTION;

END;

Const

N1: TNODE = (Next: NIL; Symbol: 'down'; value: down);

N2: TNODE = (Next: @ n1; symbol: 'up'; value: Up);

N3: TNODE = (Next; @ n2; symbol: 'right'; value: Right);

N4: TNODE = (Next; @ n3; symbol: 'left'; value: left);

Const

DirectionTable: pnode = @ n4;

In the above example, first declare an enumerated type TDIRECTION, a pointer type PNode, a record type TNode, where NEXT

Fields are PNODE types.

Then declare the constant of four record types, where the value of the next field is the value of the N1's next field given by the constant of the pointer type.

It is NIL, and so on.

Finally, a constant DirectionTable type of a pointer is declared, and its value is the address of the recording constant N4.

⒊ Process type constant

The syntax of the constant statement of the process type is as follows:

Constant Name: Process Type = Initial value;

Declare that a process-type constant only needs to give a process or function name, the given process or function must be compatible with the type of constant, that is, the number of parameters and sequences, and the type of parameters must be consistent, otherwise only NIL can be specified. .

The program is now as follows:

Type

TerrorProc = Procedure (ErrorCode: integer);

Procedure DefaultError (Errorcode: Integer);

Begin

Writeln ('Error', ErrorCode, '.');

END;

Const

Errorhandler: TERRORPROC = DEFAULTERROR;

In the above example, first declare a process type TERROC, and then define a process called DEFAULTERROR, and finally declare

A constant Errorhandler in a process type, its value is the defaulterror process.

⒋ constant constant

If you want to declare a constant of a structure type, you must give the values ​​of each component of the constructed type. In fact, we have already introduced the record class.

The constant constant, in addition to the constant of the record type, can declare the constant of array types and collection types, but cannot clear the file type

Constants or constants of record types containing file type components.

Declare that a constant of an array type must give all the elements, its syntax is as follows:

Const constant name = (component value 1, component value 2 ...] value N);

Each element of the constant of the array type is enclosed by a pair of parentheses, and each component is separated by a comma. E.g:

Type

TSTATUS = (Active, Passive, Waiting);

TSTATUSMAP = Array [TSTATUS] OF STRING

Const

Statstr: TSTATUSMAP = ('Active', 'Passive', 'Waiting');

In the above example, first declare an enumerated type TSTATUS, then declare an array type, and its subscript type is TSTATUS,

The type is String, and finally declares an array type constant of a TSTATUSMAP type. Its three elements are 'Active', respectively.

'Passive', "Waiting '.

The base type is an array of characters, and a string can be used as a constant value when declared, for example:

Type

TSTATUSMAP = array [1..11] of char;

Const

Statstr: TSTATUSMAP = 'this is way';

To declare the constant of the collection type, the value of constant is a collection constant that is enclosed in square brackets, for example:

Type

DIGITS = set of 0..9;

Letters = set of 'a' .. 'Z';

Const

Evendigits: Digits = [0, 2, 4, 6, 8];

Vowels: letters = ['a', 'e', ​​'i', 'o', 'u'];

In the above, two set types are first declared, then declare the constants of two collection types, such as EvenDigits indicate even gatherings.

Vowels represents the vowel letters.

To declare the constant of the record type, the value of each field of the record must be given, for example:

Type

TPOINT = Record

X, Y: SINGLE; END;

TVector = array [0..1] of tpoint;

TMONTH = (Jan, Feb, Mar, Apr, May, Jun, Jly, AUG, SEP, OCT, NOV, DEC);

TDATA = Record

D: 1..31;

M: month;

Y: 1900..1999;

END;

Const

ORIGIN: TPOINT = (x: 0.1; Y: 0.2);

Line: tvector = ((x: -3.1; y: 1.5), (x: 5.8; y: 3, 0));

Someday: TDATA = (D: 2; M: DEC; Y: 1969);

In the above example, a record type TPOINT is implemented. It has two fields x, y, and the type constant Origin is declared accordingly, the X word is given.

The value of the segment is 0.1, the value of the Y field is 0.2. Line This type of constant is more complicated. First, TVector is a base type for TPOINT.

When the type, the type constant line is declared accordingly, the value of the two elements is first given according to the requirements of the array type constant, each of which

The element is also a record type, so it must also give the value of each field.

Note that when the constant of the record type, the order of the field must be the same as the sequence of record type declaration, and the record type cannot be

Includes components of file type.

Comprehensive combination of various types of constants, you can find that the statement of type constants is similar to variables, in fact in the program

The medium type constant can be used as a variable, just pay attention to a point, the value of the type constant cannot be changed.

Six: variable type

⒈ What is a variable type

From Delphi2.0, a new data type Variant is caused, and its exact type is variable during the running period, that is, it can

Indicates some other types of values ​​(except the Variant type itself). Variant types are mainly used for the type of data in the compile period being uncertain.

Cordable or possibly change in the runtime. The Variant Type has the following features:

Variant types can not only express general data types such as integer, real, string, Boole, but also OLE automation

Icon, you can also represent an array of length and dimensional variables.

Variant type variable is always initialized to a special value unassigned after being declared, indicating that the variable has not been assigned, if

The value of the variable is NULL, indicating the value of the variable is unknown or wrong.

Variant type variables can appear in the same representation with data with general types, and the compiler will automate the type of turn as needed.

change.

When Variant's variable represents an OLE automation object, the characteristics and method of the object can be referenced by this variant type variable.

Wait.

Note: Although variable types provide many programming flexibility, more memory space and runtime are taken.

■ Object Pascal language data types are all described here, from the next chapter, I will tell: Object Pascal

Statement of statement

Appendix: Turbo Pascal History

Published an era

Version name

main features

1983

Turbo Pascal 1.0

Turbo Pascal 2.0

Turbo-87 Pascal

Improve the real capacity and expand the value domain

1985

Turbo Pascal 3.0

Increase graphics

Turbo BCD Pascal

Specially suitable for commercial

1987

Turbo Pascal 4.0

Provides integrated development environment (IDE), introduced unit concept

1988

Turbo Pascal 5.0

Increase debugging function

1989

Turbo Pascal 5.5

Support object-oriented programming (OPP)

1990

Turbo Pascal 6.0

Provide object-oriented application framework and library (Turbo Vision) 1992

Turbo Pascal 7.0

Object-oriented application system, more complete IDE

Turbo Vision 2.0

1993

Borland Pascal 7.0

Develop the Object Windows library,

(For Windows)

Provide support for OLE multimedia applications

1995

Delphi

Visual Pascal

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

New Post(0)