In Object Pascal, variables must have a clear data type. The data type determines the collection of variables and can participate
The calculation. Therefore, the data type is very important, and many programmers most headache are also data types.
Object Pascal supports rich data types, which is approximately 6 categories.
(1) Simple type, including ordered types and realments.
(2) String type, including short-string type, long string type.
(3) Types, including array types, file types, classes, class reference, record types, and collection types.
(4) Pointer type.
(5) Process type, treat the process and function as an object.
(6) Variant, whose type is variable in the running period.
Detailed description of the following:
⒈ Simple type
1 Order type overview:
The characteristic of an ordered type is that its value is always an integer order, and its value domain is limited.
There are 12 predefined ordered types in Object Pascal:
Integer Shortint Smallint Longint Byte Word Cardinal Boolean Bytebool WordBool LongBool Char
There are two user-defined ordered types:
ENUMERATED (enumeration type)
Subrange (sub-boundary)
For data with Deng type, Object Pascal provides a run function, which is specifically for ordered type of data, where:
ORD returns a number of serial numbers in its number.
PRED, returns a number of previous serial numbers in their number of concentrations. If this number is already the first number, and the range checks
The translation switch {$ r} is on the open position, which will trigger an exception.
SUCC, returns a number of serial numbers after its number of sets, if this number is already the last number, and the range check
Compilation switch {$ r} In the open position, it will trigger an exception.
Low, return the minimum value in the number of given types
HIGH, returns the maximum value in a given type.
2 integer in an orderly type:
Integer in an ordered type is actually a collective of all symbol integers, and Cardinal is actually all no symbolic numbers.
Recently, a variable is declared as an Integer or Cardinal. The word length and value domain depends on the CPU and the operating system, that is,
Under different machines or different operating systems, it may not. Please see the list below:
Under the 16-bit operating system (Windows 3.1), the word length and value domains of Integer and Cardinal are:
Integer: -32768..32767: 16 bits of symbol
Cardinal: 0..65535: No symbol 16
Under 32-bit operating systems (Win9x), the word length and value domains of Integer and Cardinal are:
Integer: -2147483648..2147483647: 32 signs
Cardinal: 0..2147483647: No sign 32
It is recommended that you try to use Integer and Cardinal to declare the variable.
If you accurately control the word length and value domain of integers, you can declare the variable as the following types:
Shortint: -128..127: Have a sign 8
Smallint: -32768..32767: Has a symbol 16 LONGINT: -2147483648-2147483648-2147483647: Has a symbol 32
Byte: 0..255: No symbol 8
Word: 0..65535: No symbol 16
When the variable is declared to the above five types, its word length and value domain are independent of the CPU and operating system.
Arithmetic operations for intensive numbers should pay special attention to the word length and value domain of the data, Object Pascal stipulates:
For integer constants, its precise type is the smallest type containing the value of this value.
When two different types of operands are arithmetic operation, they first convert them to the smallest value of the value of two types of all values.
Types of.
In the assignment statement, the expression on the right side of the assignment number is independent of the type of variable on the left of the assignment number.
Note: You can force the conversion to convert an integer type into another integer type (for details on the type forced conversion)
For integer constants, you can also use hexadecimal, indicating that the method is to add $ symbols in front of the number, such as $ 10 is in the 10,000 credit.
16, $ 3C is 60 in decimalization.
3 Boolean type in an ordered type:
The Boolean type is mainly used in relational operations and conditional statements, and there are four Boolean types in Object Pascal:
Boolean: 1 byte: can only be two counts 0 or 1
Bytebool: 1 byte: 8-bit signed integers
WordBool: 2 bytes: 16-bit signed integers
LongBool: 4 bytes: 32-bit signed integers
For Boolean, its value can only be 0 (false) or 1 (true), and ByteBool, WordBool and longBool can be symbolic.
Integer, value of 0 represents FALSE, non-0 value represents true.
The most commonly used use of Boolean, and ByteBool, WordBool and longbool types are for other languages such as C / C and
The Windows environment is compatible, because the Windows API is returns a Boolean value, its value may be a two-byte symbolic integer,
If you try to assign the return value to the Boolean type data, the compiler thinks that the type does not match, if the type is forced to convert,
The valid data of the return value is truncated.
In addition, Object Pascal stipulates that false is less than true, ord (false) is equal to 0, ord (true) is equal to 1, SUCC (FALSE), etc.
True, PRUE is equal to FALSE.
4 Character type in an orderly type:
The Char is a collective name of the character type, just like Integer in the integer, its precise type is Ansichar and Widchar.
Ansichar is a byte, and the character set includes an extended ANSI character.
WideChar accounts for two bytes, including Unicode characters, and the first 256 characters correspond to the ANSI character.
If a variable is declared as char, usually ansichar, but it is possible to be widechar under certain CPUs or operating systems, so
If you must use characters in your program, it is best to use it.
The SIZEOF function gets.
Object Pascal provides two functions for operations for characters:
ORD is used to return a serial number of a character.
CHR is used to convert an integer into characters of the corresponding sequence number.
Here is the concept of control characters. In Object Pascal, you can use a #nabo to a character of 0 to 255 as a character, such as:
# 0 indicates the character, an empty character of the serial number 0 in the ASCII code.
# 27 Indicates the character of the serial number 27 in the ASCII code, that is, ESC.
You can combine control characters as normal characters and strings, for example:
'My name is' # 49 # 49 # 49'Xu'. 3 # 49 characters in the string represent 3 1 1.
Note: The integer constant must follow the #, there is no space in the middle, and if several control characters are part of the string, control
There is no space between the characters.
5 Real types in simple types:
The real shape is not an ordered type because its value domain is unlimited, and the real type of data is also called real numbers, similar to the floating point in C / C .
There are 6 kinds of real numbers in Object Pascal:
Type: Active Bit: Word Heier
REAL: 11-12: 6
Single: 7-8: 4
Double: 15-16: 8
Extended: 19-20: 10
CoMP: 19-20: 8
Currency: 19-20: 8
Among them, it is generally more commonly used in REAL, SINGLE, DOUBLE.
Real numbers can also be expressed by scientific representations, for example:
7e-2 is shown that 7 multiplied by 10 is 2 times, of which 7 is called the mantissa portion, and -2 is referred to as an index portion.
12.25E 6 or 12.25E6 is shown in 12.25 multiplied by 10 by 10.
Use the scientific representation to pay attention: The index must be an integer, which can be negative, even if the mantissa is 1, it is not omitted, such as writing
E-2 is wrong.
6 enumeration type:
Enumeration Types and sub-boundary types are different from standard simple types, and the standard simple types such as integer and real-purpose, etc., whose word length and value domain and
The operations that can participate are pre-defined by the Object Pascal language, and the programmer does not have to make a type description. Enumeration type and child
The type of the boundary is customized by the user, which is also an enumeration type. If the type description is different, this type of data is expressed
Sex also different.
The so-called enumeration type is to represent a set of continuous integer constants with a limited number of identifiers. Use enumeration types to be clearer.
It is a world. For example: 7 days a week, the program uses 0, 1, 2, 3, 4, 5, and 6, respectively, one week.
Every day, of course, it is ok, but the program is very intuitive, maybe you are in the phase, can you express it with a constant? Yes, but use enumeration
Types have its own unique.
Declare an enumerated type syntax is like this:
Variable name = (enumeration list)
Enumeration list: identifier 1, identifier 2 ... identifier N
The identifiers in the identifier list are separated from each other, which lists the scope of the enumeration type, that is, one
An enumerated type of variable whose value is always one of the listed identifiers, which is the original type of this type.
Because, this is a feature of enumeration type. E.g:
Type
Dayofweek = (Sun, MON, TUE, WED, THU, FRI, SAT);
Object Pascal stipulates that the value of the first identifier is 0, the value of the second identifier is 1, and push it according to the class.
Note: On the above example, only declares an enumeration type dayofweek, but the type cannot participate in the program directly in the program. It seems like INTE-
Like the ger type, you must also declare a DAYOFWEEK type variable,
E.g:
Var mydays: dayofweek;
This declares an enumeration variable MYDAYS, myDays of a DayOfWeek type, always in one of the identifier list.
Of course, in order to simplify the program, you can declare the type declaration and variables to first, for example:
Var mydays: (Sun, MON, TUE, WED, THU, FRI, SAT);
Note when declaring enumeration types and enumeration variables:
The elements of the enumeration can only be an identifier, and the naming of the identifier must comply with the Object Pascal regarding the identifier, such as the following sound.
Ming is wrong:
Type dayofweek = (0, 1, 2, 3, 4, 5, 6, 7); (identifier cannot be headed)
Type dayofweek = (for, OF, end, do) (Identifier can't be a reserved word)
The same enumeration element cannot appear in multiple enumerations at the same time, for example:
Type Colors = (Red, Blue, Green, White, Black);
Anothercolors = (YELLOW, LINE, SILVER, GREEN);
There are Green elements in the two types above, which is not allowed.
You can't use the enumeration element to participate in the operation, to enumerate the type variable MyDays as an example, if there is a program
X: = fri * 4;
This is wrong, you can only use an enumerated element to assign a variable variable, for example:
Mydays: = fri;
Several standard functions are provided in Object Pascal for operation of the enumeration variable, where the PRED function is used to return the designated element.
The previous element, the SUCC function is used to return the latter element of the specified element, such as (as an example of MyDays variable):
PRED (Fri) is equal to THU, SUCC (Sun) is equal to MON.
There is also a function ORD to return the value of the specified enumeration element (serial number), for example: ORD (WED) is equal to 3
7 sub-boundary type:
The feature of enumeration is that its value is the number of flexible, while the child type is characterized by its value is always in one interval (all of the value
There may be difficult to list out.
Declare the syntax of a sub-boundary type is like this
Sub-bound type = lower bound constant .. context constant
The type of lower bound constant and the upper boundary constant must be the same ordered type, such as integer, Boolean, character type, but also an enumeration type, but
Can't be a real shape, the type of these two constants is also known as the child host type.
The program is now as follows:
TYPE Letters = 'a' .. 'Z';
Month = 1..12;
The host type of the sub-boundary can also be an enumeration type, but it must be declared before the type of sub-boundary type of the host type is enumerated.
Type, for example:
TYPE MONTH = (Jan, Fed, Mar, Apr, May, Jun, JUL, Ang, SEP, OCT, NOV, DEC); {Declaration an enumeration type}
Months = jan..dec; {Declaration a sub-boundary type}
Like the enumeration type, the type cannot participate directly, and must also declare a variable of a sub-boundary type, for example:
Var mymonths: months;
To simplify the program, you can also match the type declaration and variables to one, such as: var myletters: 'a' .. 'Z';
Notice and use the child type to pay attention to such a few points:
The type of uplink constant and the lower boundary constant must be consistent and are all ordered types.
The sub-boundaries of the sub-boundary variables can be used to refer to its host type.
The value of the sub-boundaries variable is a range, and if this range is exceeded during the operation, the program will result in an error.
Delphi's documentation mentioned: The upper and lower boundaries of the sub-boundary type can also be an expression.
⒉ String type introduction
Object Pascal string type is divided into short strings and long strings.
The so-called short-string refers to a character sequence that is more than 255 characters in a string length. When the status of the compilation switch is {$ h-}, use
The word string declares is a short string, regardless of the state of compilation switch $ h, the variable declared with ShortString is always
Short string type, program example, as follows:
Var mystring1: shortstring;
Var myString2: string [100];
In the above example, there is no symbolic integer enclosed in square brackets after the word string, indicating that the maximum length of the string is 100. When putting a character
When serving a variable of a short-string type, the portion of the maximum length will be cut off.
Each character in the short string can be accessed through the character string name, note that although the index starts from 0, but indexes
0 The character stored is the actual length of the string, that is, the calling ORD (S [0]) is equivalent to calling Length (s). Real characters are
Start at the index 1.
Since the length of the short-string is dynamically changed, you can use the Low and High function to get the maximum serial number and minimum serial number of the string, low
The return value is of course 0, and the return value of HIGH is the maximum length of the declared string.
The short-string type is mainly compatible with the early Delphi version, in Delphi1.0, there is no concept of a long string.
The so-called long string, its length is almost unlimited, theoretically reaches 2GB, which is actually only limited by the computer's memory. To declare one
For a long string, use a predefined identifier ANSISTRING or change the compilation switch to {$ h } (default)
String to declare.
Compared with the short string, in addition to the maximum length, the long string is automatically initialized into an empty string after the declaration (with
The character variable is a global or partially unrelated), and each other in the long string must be started from the index 1, long words
Fu Lu has a character that indexes is 0, to obtain the actual length of the long string, can only call the LENGTH function.
There is also a feature of the long string, that is, the long string automatically adds a NULL character after its last character indicates the end of the string (note:
NULL characters are not part of a long string), this is a bit like character pointer type (PCHAR), so you can be very convenient
Convert a long string type to PCHAR type, syntax PCHAR (S), where S is a long character string expression, so long words
The string is converted to a pointer, pointing to the first character of the long string, even if the long character string is empty, it can always be obtained after the conversion
Get a pointer to characters ending with NULL characters.
The string ended with NULL is primarily used to compatible with the Windows API or other languages. Please see the example below:
Suppose Caption and Message are long strings,
Caption: = 'Hello World';
Message: = 'this is a test of long string'; MessageBox (0, Pchar (Message), Pchar (Caption), MB_OK;
You can also convert a long string into a non-type pointer. The syntax is Pointer (s), where S is a long string expression,
The result of the conversion is a pointer to the first character address, which is different from the PCHAR forced conversion, if the long string is empty, turn
The result is NIL.
In general, whether it is using PCHAR or POINTER, the string pointing to the pointer to the pointer is usually transmitted as a read-only string.
Windows's API or other language writes.
Object Pascal allows a string and a long string mix to occur in the same expression, and the compiler will automatically need to convert.
Finally, pay attention: The string variable must be enclosed in a pair of single quotes. If the string itself has single quotes, this single quotes use two
Continuous single quotes, for example:
'His name is' fang' ''
This chapter ends here, I will tell: Construct Type and Pointer type
Appendix 1: All Keywords and Reserved Words in Object Pascal Languages (Reserved Words)
★ This table is provided by Italy's famous Delphi writer: Marco Cantù provides ★
I didn't translate "role" into Chinese, I want everyone to know more about the appearance of these words in Delphi.
Keyword Role
Absolute Directive (Variables)
Abstract Directive (Method)
And Operator (Boolean)
Array Type
As Operator (RTTI)
ASM STATEMENT
Assembler Backward Compatibility (ASM)
AT Statement (Exceptions)
Automated Access Specifier (Class)
Begin Block Marker
Case Statement
CDECL Function Calling Convention
Class Type
Const Declaration or Directive (Parameters)
Constructor Special Method
Contains Operator (SET)
DEFAULT DIRECTIVE (Property)
Destructor Special Method
Dispid Dispinterface SpecifierDispinterface Type
Div Operator
Do Statement
Downto Statement (for)
Dynamic Directive (Method)
Else Statement (if or case)
End Block Marker
Except Statement (Exceptions)
Export Backward Compatibility (Class)
Exports Declaration
External Directive (Functions)
FAR Backward Compatibility (Class)
FILE TYPE
Finalization Unit Structure
Finally Statement (Exception)
Forsti STATEMENT
Forward Function Directive
Function Declaration
Goto Statement
IF Statement
Implementation Unit Structure
IMPLEMENTS DIRECTIVE (property)
IN Operator (SET) - Project Structure
INDEX DIRECTIVE (Dipinterface)
Inherited Statement
Initialization Unit Structure
Inline Backward Compatibility (See ASM)
Interface Type
Is Operator (RTTI)
Label Declaration
Library Program Structure
Message Directive (Method)
Mod Operator (MATH)
Name Directive (Function)
Near Backward Compatibility (Class)
NIL Valuenodefault Directive (property)
NOT OPERATOR (Boolean)
Object Backward Compatibility (Class)
Of statement (case)
On Statement (Exceptions)
Or operator (Boolean)
Out Directive (Parameters)
Overload Function Directive
Override function Directive
Package Program Structure (package)
Packed Directive (RECORD)
Pascal Function Calling Convention
Private Access Specifier (Class)
Procedure Declaration
Program Program Structure, PROGRAM
Property Declaration
Protected Access Specifier (Class)
Public Access Specifier (Class)
Published Access Specifier (Class)
Raise Statement (Exceptions)
Read Property Specifier
Readonly Dispatch Interface Specifier
Record Type
Register Function Calling Convention
Reintroduce Function Directive
Repeat Statement
Requires Program Structure (package)
Resident Directive (Functions)
ResourceString Type
SafeCall Function Calling Convention
SET TYPE
SHL Operator (MATH)
SHR OPERATOR (MATH)
Stdcall Function Calling ConventionStored Directive (property)
String Type
Then Statement (if)
Threadvar Declaration
To Statement (for)
Try Statement (Exceptions)
Type Declaration
Unit Unit Structure
Until Statement
Uses Unit Structure
Var Declaration
Virtual Directive (Method)
While Statement
WITH STATEMENT
Write Property Specifier
Writeonly Dispatch Interface Specifier
XOR Operator (Boolean)