Note: This article has been translated by Fangyuan (Shanghai) and creation. If there is any improper place in the text, please discuss the author's letter
(E-mail: ilovejhx@163.net)
■ The production of this article is supported by the Macula Working Group! Thanks here!
One: "Construction Type" in the Object Pascal language is as follows:
Array
⒉ collection
⒊ record
⒋ ⒋
The following details are described in detail.
Array
The array is a sequence of some elements with the same type, and each element in the array can be plus a serial number by array.
access.
How to define an array type
In Object Pascal, you want to define an array type, which is as follows:
Array type = array [next label ...] OFF type type
You can define an array like this:
TYPE myArray = array [1..100] of real;
Indicates that a subscript type is an integer, a total of 100 elements, the element subscript from 1 to 100, each element type is REAL, type identifier
It is the array type of MyArray.
Object Pascal allows the type of subscript that can be an integer, character type, Boolean, enumeration type, etc., and the type of element can be arbitrary.
For example, you can subscribe to an array type of enumeration type:
TYPE AX = (X, Y, Z);
MyArray = array [ax] of integer;
How to define array variables
The front has been emphasized many times that types of variables are two different concepts, and the program cannot directly use the type, but must pass the variable statement.
One variable, for example:
Var Array1, Array2: MyArray;
Two array variables Array1 and Array2 are defined in the above example, and their types are MyArray (previously defined as an array type).
Of course, you can define the definition of the array type and the array variables for two, which can simplify the program, as in the following example:
Var Array1: array ['a' .. 'z'] of real;
To access the elements in the array, you can use the array name to add square brackets, square brackets are the declaration values of the elements, such as Array1 [13], etc.
Note: The debt value in square brackets must meet the definition of the subscript type in the array type, and its type must be consistent with the subscript type, its value is taken
The value is within the range. In addition, the subscript can be an expression.
The standard functions that use the Object Pascal can mention a minimum downbound value and maximum subscript value of an array.
In addition, we take the type of elements in the array as the type of the entire array, and the calculations of the number of components can participate in their elements.
Multidimensional Arrays
The above is a one-dimensional array, the so-called one-dimensional array, in the form, is only one subscript, such as x [1..2], y [3..100]. More
Dimensional groups are the elements in the index group itself or an array, and the general form is:
TYPE array identifier = array [subscript type 1] of array [subscript type 2] OF element type
You can also write the above form to the following form:
TYPE array identifier = array [subscript type 1, subscript type 2] OF element type
Now you can define such a two-dimensional array variable:
Var Array1: array [1..5, 1..8] of integer;
Indicates that there are 5 elements of the array, each element is an array consisting of eight elements.
You can view multi-dimensional arrays as a matrix, where the subscript 1 is line, the subscript 2 is column, so that the elements of the multi-dimensional array can be written: Array1 [2,3] means accessing the second line of the third column Elements.
Zero-based group
The so-called zero-based group is an array starting from 0, for example:
Array [0..5] of char
The zero-based array is mainly used to store a string end of the NULL character, and the element type is compatible with the PCHAR type assignment.
The API that calls Windows needs to be used to end with NULL characters.
Overall assignment of arrays
Suppose there are two arrays, array1 and array2, which are defined as follows:
Var array1, array2: [1..10] of char;
If you want to assign the value of each element in Array2 to the element in Array1, it is generally used to use the for statement, for example:
For i: = 1 to 10 do array1 [i]: = array2 [i];
Object Pascal language allows only one assignment statement, you can assign the value of each element in Array2 to the element in Array1 accordingly.
The program writes this:
Array1: = array2;
Note: The overall assignment between the arrays is only applicable to an array of the same array type, even if the two arrays look exactly the same, it cannot be assigned as a whole.
E.g:
Var array1, array2: array [1..10] of integer;
Var Array3: array [1..10] of integer;
Among the above three arrays, Array1: = array2 is legal, but Array1: = array3 is illegal. Because because they are not simultaneous
Ming, therefore will be considered two different types, and the problem of type compatibility is generated. However, through a FOR loop can be assigned between the two,
Therefore, their base classes are assignable.
If this is declared:
TYPE myArray = array [1..10] of integer;
then:
Var Array1, Array2: MyArray;
Var array3: myarray;
At this time, the three arrays are all types, so they can be assigned to all assignments.
Put the array as a process or function
Object Pascal allows the array to pass to the process or function as a meticulin, and allows the type definition and transfer of the array to one, program
Examples are as follows:
Procedure myproc (var array1: array [1..10] of integer;
Begin
{Code}
END;
In the above program, the definition and delivery of the array is carried out, and Array1 is transmitted as a parametric, meaning that it can change this during the process.
The value of the parameters. In addition, arrays can also be used as a function of the return type.
Open array
The Object Pascal language implements an open array; the so-called open array is that the length is the length when the array is transmitted to the process or function.
Uncertain, this can be transmitted to the array of different lengths when calling this process or function.
Please refer to the following demonstration procedures for the definition and usage of open arrays;
{Define two array variables in two lengths}
Var x1: array [1..10] of real;
Var x2: array [1..20] of real;
{Definition of the process, its shape is an open array}
Procedure myproce (x: array of real);
Begin
{Process body}
END;
Begin
MyProc (x1);
MyProc (x2);
END;
Note: The type of arguments must be the same as the type of the shape parameter group, and the above example is a REAL type.
In the process or function body, the length of the open array varies with the length of the argument, and always zero-based group, that is, its lower boundary
0, the upper bound is a minimum of the elements in the actual parameters, such that the sequence number of the elements in the informants may not participate. If the argument is just a simple
The single variable is regarded as an array of only one element.
Because the number of elements of the open array is uncertain, you can call the HIGH function to get the current maximum winner value.
Note: The general array can be assigned as a whole, but as the open array of the ginseng is not allowed to assign a whole, you can only access its elements.
You can pass an open array as a parameter to another process or function in the process or function.
Open arrays can be used as a numerical parameter, a constant parameter or a variable parameter. Description of these parameters can see the process and function introduction
When an open array is a numeric parameter, the compiler opens up a copy of the array in memory, and then releases the process or function to exit.
This area, so when the argument is a large array, the problem that the stack overflow can occur. And open arrays as constant parameters or variables
When the number, the compiler is only an address, of course, this also brings a new problem, if the parameter is modified in the process or function,
In fact, it is modified to be inactive.
If the type of open array is a character, you can pass a string as a party to it.
In the above example, the transmitted argument is a defined array variable, in fact, Delphi also allows more direct calls, as long as it is passed
Element values are enclosed in square brackets, and the elements are separated from the elements, and the program is shown below:
MyProc ([1, 2, 3, 4]);
MyProc ([1, 2, 3, 4, 5, 6]);
This way, you don't need to define array variables in advance and assign values to array variables.
Dynamic array
Dynamic arrays allow you to define the length of the array, and dynamically allocate the storage space of the array in the program so that arrays are used more flexible.
characteristic.
The syntax defined in a dynamic array is this:
Type name: Array Of data type
Here you can see that you don't have to declare the length of the array, and before using the dynamic array in the program, you need this to write:
SETLENGTH (number of group names, number of array elements)
What is most important is that a dynamic array is a zero-based group, and the first element should be 0.
⒉ collection
The collection is originally the concept of mathematics. The concept of introducing a collection in Object Pascal is to bring a set of related objects as a whole.
Participate in the operation, each of which is called a collection element. In fact, understand the collection concept in mathematics to understand the Object Pascal language
The set type of the collection is helpful.
Collection type statement
Declare a collection of syntax as follows:
TYPE Collection Identifier = Set of base class
Where SET is reserved, the base type can be any ordered type such as integer, Boolean, characters, enumeration and sub-boundary, but cannot be
Real or other constructive types.
The program is now as follows:
Type set1 = set of 'a' .. 'Z'; {base type is sub-boundary}
Set2 = set of char; {base type is character type}
Set3 = set of (Jan, Feb, Mar, APR); {base type is enumeration type}
The type itself cannot participate in the operation, but also a variable of a collection type, for example:
Var myset: set1;
To simplify the program, you can also match the type of the type and variables to one, for example:
Var myset: set of 'a' .. 'Z';
It is necessary to specifically remind this: the difference between the structure type and enumeration type into the type of sub-boundary is that the variable of constructive type is a whole, and enumeration
The variables of the type and sub-boundary at the same time, its value is an element, so, although the type of enumerated type and sub-boundary type is more complicated.
But still
It belongs to the simple type.
A set type of value domain is actually a subset of the value domain of its base type, which may be empty set, and a set type of data.
The domain can also be a subset or empty set of a collection type value field.
Note: Object Pascal stipulates that the elements of the collections cannot exceed 256, and the upper and lower bounds of the value domain of the base type must be in the 0 to 255
Inside, the statement below is wrong:
TYPE SET1 = set of integer;
Because the number of elements in the collection is far more than 256.
Representation method for collecting type constants
Collective constants are expressed in a set of elements in square brackets, for example:
TYPE DIGITS = set of 0..9;
Letters = set of 'a' .. 'Z';
Const
MYDIGITS: DIGITS = [1, 3, 5, 7, 9];
Myletters: letters = ['a', 'b', 'c', 'd', 'e', 'f'];
In the above, two set types of Digits and Letters are declared, then declare two collection constants, where MyDigits are DIGITS types.
Constants, according to the above provisions, its value domain can be a subset of the value domain of DIGITS, such as odd part between 0 and 9, and myletters are
The constant of the Letters type, its value domain is the header of Letters.
You can also declare an empty set of DIGITS types, for example:
Const mydigits: Digits = [];
Note: Some collection constants look at elements, actually empty sets, for example:
Const myletters: letters = ['Z' .. 'a'];
Combine can participate
The collection type can participate in assignment, relational operations, and logical operations.
(1) Assignment operation
Like a simple type, declare a set type of variable, this variable has no clear value, must be assigned by assigning statements.
A value, for example:
Var set1, set2: set of 1..10;
In the above, although two collection variables are declared, it is not a collection of variables set1 and set2 representing a collection of 1 to 10, but also must
Value, for example:
Set1: = [1, 3, 4, 6, 9];
Set2: = [1..3, 5..8];
Such set1 is a collection of 1, 3, 4, 6, and 9, and set2 is a collection of 1, 2, 2, 5, 6, 7, 8.
Assigning values for collection variables should pay special attention to assignment compatibility.
(2) Relationship operation
The calculation of the collection type, follow the rules of the collections in mathematics, the results of the operations of the Boolean.
The relationship between the collections is nothing more than judging the relationship between two collections, including = (equal to), <> <> <> (greater than or equal) and <= less than or equal, first we define several rules:
The so-called set A is equal to the collection B, which means that the elements in the set A are exactly the same (no matter the order of the elements), otherwise it is unequal.
For example, for example:
[1, 2, 3, 4] = [1, 3, 4, 2]
[1, 2, 3, 4] <> [7, 8, 9]
If the elements of the collection B can be found in the collection A, we call a collection A greater than or equal to the collection B, and vice we call B greater than or equal to the collection A,
E.g:
[1, 2, 3, 4]> = [1, 2, 3]
Note: You cannot use> or Use ORD, PRED, SUCC. There is also a special operator in Object Pascal in which an element is used to refer to the relationship operator in the collection. (3) Logic operation Detailed description of the logical operation is also called a collection of collections, reference relationship operations. ⒊ record The record type is very similar to the structure of the C language. In the first few data, the type of files to be introduced later, the element of the constituent type It must be the same type of data, but in reality, there is often a need to concentrate some data types, and as a whole For example, a typical example is the customer's registration form, which is generally consisting of the following information: Customer Number (Integer) Customer name (string type) Address (string type) Have you paid (Boolean) Friends who are familiar with the database may think that Object Pascal record types are similar to the database record concept, and the elements in the record type can be To understand the fields in the database, in fact, Object Pascal is using the concept of records and fields in the database. Record type declaration Declare a syntax of a record type: TYPE Identifier = Record Field 1: Variable type; Field 2: Variable type; ... Field N: Variable Type END; For example, the above customer table can declare this TYPE CUSTOMER = Record Custnum: integer; Name: String; AddR: string; IFPAY: BOOLEAN; END; In the above, a Customer type record is declared, which consists of 4 fields, each of which has its identifier and data classes. Type, they occupy different areas in memory, and we refer to these few fields as recorded fixed parts. A record type can be not available in a field. The type itself cannot participate directly, but also a variable of the record type, for example: Var Cust1, Cust2: Customer; In the above, two Customer type variables Cust1, Cust2 were declared. To simplify the program, you can also match the type of statement and variables to one, but you recommend you because of the type description of the record type. Still separately. Note: The fields in the record can be any type, including simple types and constructive types, or even another record type. How to access the fields in the record declare a record type variable, you can access each field in the record, in the Object Pascal, visit a record in the record The syntax of the field is this: record the variable name. Field name, for example: Cust1.name:='smith '; Cust2.ifpay: = TRUE; Note: The arithmetic and assignment of the participants is determined by the data type of the field. Overall assignment of records Although each field in the record can perform various legal operations, records can only be assigned as a whole, that is, a record The whole is assigned to another type of consistent record (consistent with the reference type and assignment), such as: Var Cust1, Cust2: Customer; Cust1: = CUST2; Note: The record variable on the right side of the assignment must be valued. The so-called value is that each field of the record has been assigned. Record and array Recording and arrays are two types of structural types that are different. The elements in the array are the same, but the number of elements is variable in a certain range. The field in the record can be insect, but the field The number is fixed. The elements of the array are accessed by array variable name plus brackets and subscripts, and the fields in the record are added to a small dot and word named record variable name. Segment name is visited. When describing complexity is always, the array and records are often used, such as a number of students in a class, using one for each class. The record is described, which requires constructing an array type of an element type as a record. Yes, this is the table of the database. WITH statement As mentioned above, the fields in the recording variable are accessed by recording the variable name plus a small dot and field name, if there are many Access field, you have to write a variable name each time, can you simplify? Object Pascal provides a with statement, its syntax is: With record variable name DO ... Dous after DO is a single statement (a single statement is not significant), or it can be a composite statement (to be enclosed in Begin and End), In these statements, access to the field does not need to add a record variable name, and the program demonstration is as follows: WITH Cust1 DO Begin Custnum: = 2; Name: = 'zhang'; IFPAY: = true; Addr: = 'this is a test'; END; The WITH statement can also have a with statement, which is called with nested, for example: WITH Cust1 DO WITH CUST2 DO ... Nested WITH statement can be short-handed into the following form: WITH Cust1, Cust2 Do ... However, when using the nested bookmark, pay attention to avoid the erliness of field access, such as one of the following records: TYPE REC = Record X: integer; Y: integer; Ax: Record X: Real; Y: real; END; END; Var MyRec: REC; In the above example, a record of a REC type is declared, where the AX field itself is a record, and the field of AX is also called X and Y, the difference is Their type is different from the type of X and Y in REC, which is allowed in Object Pascal. We don't have to write a procedure in writing: With myrec do Begin X: = 1; With ax do X: = 1.0; END; If we replace it into a short-written form, it may generate an erriness: With myRec, Ax DO Begin X: = 1; X: = 1.0; END; In the above example, X-in-final is Integer or real. In addition to the type of type, WITH is applied to the characteristics and methods of accessing the Delphi component more. Variable part recorded The fields of the previous record have a certain identifier and data type, but in real life, there is often this situation, such as in a resume. There is this first column "Participation Work Time", if you fill out, then fill in time, if you want to work, let it blank. In order to describe such a history, the variable portion of the record is used in the declaration record type, the syntax of the variable portion is like this: CASE Identification field identifier: Identification field type of Constant 1: (Field List 1); Constant 2: (Field List 2); ... Constant N: (Field List N); For example, we describe the above history table with the following record type: TYPE REC = Record Name: = String; Indeger; Birthplace: String; Case ifWord: Boolean of True: (whenWork: string); FASLSE: () END; In the above example, we use ifWord to determine if it works. If it is WhenWork to describe working hours, if not, there is nothing. Note: The constants in the type declaration must be mutually exclusive, and their types must be an ordered type that is compatible with the identification field type. In addition to The type of the field of the variable section cannot be a long string type and variable type (Variant), and it is also unable to contain a long string or variable component. Type. ⒋ ⒋ The various data types introduced earlier are only meaningful in the program running phase, and once the program exits, these data no longer exists, and the file type It is a permanent data type because the data of the file type is on the disk of the computer. Second, the various data types described above, their data capacity is limited, that is, the limitations subject to the data type itself, also subject to computer memory Restrictions, and the data of the file type has little capacity. File type declaration The so-called file is a linear sequence composed of elements with the same class type, which has two forms of grammar: TYPE type identifier = file; or TYPE type identifier = file of base type The previous form declares an unomayed type of file type, mainly used to lower the files on the disk for low I / O. The latter form declares a file that specifically type, the base type can be any simple type and constructive type, but cannot be a file type or The type of constructor of the component component cannot be class types and classes. Quote type. The program is now as follows: Type Student = Record Name: String [15]; Indeger; END; STUDENTFILE = file of student; Numberfile = file of integer; Tempfile = file; In the above example, first declare a record type Student, then declare a base type is the file type StudentFile of Student, one base class The type of file type Numberfile, a category of file type TempFile, a file type of INTEGER. Like other types, the type cannot be used directly in the program, and the file type variable must be declared, for example: Var myfile: studentfile; In the above example, a constitfile type variable myfile is declared. To simplify the program, you can also match the type declaration and variables to one, for example: Var myfile: file of stdunt; There is a predefined file type text in Object Pascal. If a variable is declared as a text type, it means that this file is made by the ASCII character group. Focus file. File type operation The file type is a type of construct, that is, the file type is a holistic concept, and during programming, an element constituting file is often required. Operation, here is introduced herein the concept of "file buffer variable". Object Pascal Automatically gives this file variable from a "file buffer variable" to this file variable, the type of file. The base type of the file variable is consistent, for example: file variable MyFile's "file buffer variable" type is the type of record, which is written as myfile ^, Taking the "file buffer variable" also known as the file pointer, only the elements currently referred to by the file pointer are visible. With the file pointer, we can access each element in the file, Object Pascal provides some standards and functions for files. Pointers operate such as moving pointers, read data, write data, and the like. After declaring a file variable, first assign a file name with AssignFile, then open this file with RESET or REWRITE, this time The file pointer points to the first element of the file, to move the file pointer, use the SEEK process, which can move the file pointer from the current location N elements. To read or write the current element, use the READ and WRITE procedures, for the ASCII file, you can use Readln and Writeln. The following is an example of the operation of the file type: VAR f: File of Integer; I: integer; Begin Assignfile (f, 'xxh.dat'); ReWrite (f); {establish and open new file} I: = 0; While (i <10) DO WRITE (F, I); Closefile (f); END; In addition, the files in Object Pascal are random, which means that you can read and write the same file, but pay attention to changes in the file pointer. After the file is functioning, you must use the closefile process to close the file. Finally, all processes and functions for file type operations are defined in the System cell, and they are confused with the file management routine of the SYSUTILS unit. The latter is mainly dealing with the file handle. 2: "Pointer Type" in the Object Pascal language ⒈ ⒈ ⒈ type statement Pointer type declaration The pointer type is more difficult to understand in any language is also a more flexible data type, and the pointer is usually the memory address of the variable it refers to. The grammar of the declared pointer is as follows: TYPE pointer type identifier = ^ Class Type; The program is now as follows: Type BYTEPTR = ^ byte; WordPtr = ^ Word; In the above example, two pointer types are declared, one is byteptr, pointing to the Byte type data, the other is WordPtr, pointing to the Word type data. Object Pascal does not mean that the base type must be declared in the previous, or only an identifier, then declare the base type within the same block. After declaring the type of pointer, you can declare the variable of the pointer type, for example: VAR BP: BYTEPTR; WP: WordPtr; To simplify the program, you can also merge the declaration of the type of declaration and variables together, for example: VAR BP: ^ Byte; The base type referred to by the pointer can be a simple type or a structure type, for example: TYPE Student = Record Name: String; Indeger; SEX: (Man, Woman); END; Var stuptr: ^ student; In the above example, a pointer variable constudptr pointing to the record type Student is declared, and the record class can be represented by STUPTR ^ in the latter program. Dynamic variables of STUDENT, to access the Name fields, can be written as stuptr ^ .name. Here, the concept of dynamic variables is introduced, and the dynamic variable is constituted, and one ^ symbol is added with the pointer type variable identifier, it constitutes the pointer. Dynamic variables of the base type pointed to the above examples are the dynamic variables of stuxt in the above example. (In the Delphi program, many programs are used to doing so. TYPE PSTUDENT = ^ TSTUDENT; TSTUDENT = Record Name: String; Indeger; SEX: (Man, Woman); END; ) ⒉ ⒉ ⒉ In fact, the simple usage of the pointer has been introduced, and several processes and operators for pointer operation are also introduced. NEW is the standard routine in Object Pascal, which is used to assign dynamic variables in the stack of applications. The address of the area is assigned to the pointer variable, and the size of the assigned area is determined by the type referred to by the pointer. If the application's stack is not enough Enough space will trigger an EoutofMemory exception. The declaration of the New process is as follows: Procedure New (VAR P: POINTER); Where P is a pointer variable, after the New process is called, the program can use the P ^ as the dynamic variable of the type of pointer. Accordingly, when the program is no longer needed to use a dynamic variable, the standard routine Dispose should call the dynamic variable created by NEW and release it. Allocated space. The program is now as follows: Type PListentry = ^ TListentry; TListentry = Record Next: plistentry; Text: String; Count: integer; END; VAR List, p: plistentry; Begin ... NEW (P); P ^ .next: = List; P ^ .text: = 'Hello World'; p ^ count: = 1; List: = P; Dispose (P); ... END; In the above example, first declare a pointer type plistentry, pointing to the TListentry type, the Declaration of the TListentry type is below, is a Record type. Then declare two PListentry type variables List and P. There is also a routine getMem in Object Pascal, which is similar to New, and is also assigned a zone for dynamic variables in the stack of the application, and assigns the address of the area to the pointer variable, the GetMem routine is specified. The size of the distribution area, the declaration of getMem is as follows: Procedure getmem (var P: Pointer; SIZE: Integer); Where P is the pointer variable, size is byte. Accordingly, the dynamic variable created with getmem must be deleted with the FreeMem routine and release the assigned space. There is also an @ operator in the Object Pascal language, used to get the address of the operand, the operand can be variables, processes, functions, or class types. Method, please refer to the operation about @ 操作 操作 symbol. There is a special reserved word nil in Object Pascal, which is an empty pointer, when the value of the pointer is NIL, indicating that the pointer does not To any dynamic variable. Note: You cannot use a value of NIL to change Quantity to access dynamic variables. In addition to the value of the pointer, the pointer variable can also be equally or unequal comparison, which is limited to the type-compatible pointer variable. In two The pointer points to the same object (not the same type) pointer equal. ⒊ None type pointer None type pointer is pointed to the pointer variable declaration does not have a base type, and there is no type of pointer to declare: Var PTR: PTR: POINTER; The role of the non-type pointer is that it can point to any type, but for the non-type pointer, you cannot use similar PTR ^ to reference it. Dynamic variable.