Delphi程序设计教程第3版by杨长春(2)

第2章 Delphi语法基础

Delphi是一种完全面向对象的开发平台,它以面向对象的程序设计语言 Object Pascal作为其程序设计语言。Pascal语言是世界上使用最广泛的程序设计语言之一,是一种结构化的程序设计语言,它具有丰富的数据类型、严谨的语法规则及高效的编译器等特点。Object Pascal 语言是Pascal语言的面向对象的扩展,它在传统的Pascal语言基础上主要增加了面向对象的特性、增加了若干数据类型、对编译器进行了改进等,使之成为一个完善的面向对象的编程语言。

保留字与标识符

任何一种语言都有自己的一套符号和语法规则,程序设计语言也是如此。在Object Pascal语言中,符号由一个或多个字符所构成,是最基本的语言元素。

标识符

标识符是Object Pascal语言中各种成分的名称,这些成分包括变量(Var)、常量(Const)、类型(Type)、过程(Procedure)、函数(Function)、方法(Method)、单元(Unit)等,要在程序中使用这些名称,必须首先用标识符说明它们。

标识符可以分为三类:标准标识符、自定义标识符和限定标识符。

1.标准标识符

标准标识符是Object Pascal语言预先分配给标准函数、标准过程、标准类型、标准常量,以及标准文件使用的标识符,如下所示。
(1)标准常量,如False、Maxint、True等。
(2)标准类型,如Boolean、Char、Real等。
(3)标准函数,如Sin,Cos、Abs、Arctan等。
(4)标准过程,如Dispose、Get、New、Pack、Put等。
(5)标准文件,如Input、Output等。
2.自定义标识符
自定义标识符是程序员根据程序设计的需要,自己定义的常量、变量、类型、函数、过程等所取的名字。自定义标识符可以由任意长的一个不带空格的字符串组成,包括字母A∼Z、a∼z、数字0~9和下划线_等。
在Object Pascal语言中,定义标识符需要遵循以下规则。
(1)标识符不区分大小写;
(2)标识符只能以字母或下划线开头,不能以数字开头;
(3)标识符可以是任意长度,但只有前225个字符有效;
(4)标识符中间不允许有空格:
(5)不允许使用Object Pascal 语言的保留字作为标识符。
例如,ABC、K_2、calculatevalue、CalculateValue和CALCULATEVALUE都是合法的标识符,但后面三个标识符被认为是同一个标识符。而9A、A&B、MY teacher和rèsΩ等都是非法的标识符。因为9A用数字开头,A&B使用了非字母、非数字和非下划线的符号&,MY teacher含有空格,resΩ使用了字符Ω。
3.限定标识符
在Delphi程序中可以引用多个单元,而各个单元中的全局变量、函数、过程等可能会同名,所以在程序中引用它们时需要使用限定标识符来区分它们,如下述语句:

Var
Y:real;
Y:=System.cos(pi);

其中System称为限定符,它限定语句中使用的cos标识符为System单元中声明的标识符。而System.cos称为限定标识符。

保留字(Reserved Words)和指令符

保留字是由系统规定的、具有特定意义和用途的单词,如and、begin、end等。在编程时保留字不能被重新定义或作他用,Object Pascal语言中定义的保留字,如下所示(Delphi中没有out)。

and#Logical_.28Bitwise.29_Operators "Expressions (Delphi)") end#Compound_Statements "Declarations and Statements (Delphi)") interface#The_Interface_Section "Programs and Units (Delphi)") record#Records_.28traditional.29 "Structured Types (Delphi)") var#Parameter_Semantics "Parameters (Delphi)")
array#Arrays "Structured Types (Delphi)") except#Raising_and_Handling_Exceptions "Exceptions (Delphi)") is repeat#Repeat_Statements "Declarations and Statements (Delphi)") while#While_Statements "Declarations and Statements (Delphi)")
as
exports label#Goto_Statements "Declarations and Statements (Delphi)") resourcestring with#With_Statements "Declarations and Statements (Delphi)")
asm file#File_Types_.28Win32.29 "Structured Types (Delphi)") library****3 set#Sets "Structured Types (Delphi)") xor#Logical_.28Bitwise.29_Operators "Expressions (Delphi)")
begin#Compound_Statements "Declarations and Statements (Delphi)") finalization#The_Finalization_Section "Programs and Units (Delphi)") mod#Arithmetic_Operators "Expressions (Delphi)") shl#Logical_.28Bitwise.29_Operators "Expressions (Delphi)")
case#Case_Statements "Declarations and Statements (Delphi)") finally#Try…finally_Statements "Exceptions (Delphi)") nil "Pointers and Pointer Types (Delphi)") shr#Logical_.28Bitwise.29_Operators "Expressions (Delphi)")
class "Classes and Objects (Delphi)") for#For_Statements "Declarations and Statements (Delphi)") not#Logical_.28Bitwise.29_Operators "Expressions (Delphi)") string
const function "Procedures and Functions (Delphi)") object#Object_Types "Classes and Objects (Delphi)") then#If_Statements "Declarations and Statements (Delphi)")
constructor "Classes and Objects (Delphi)") goto#Goto_Statements "Declarations and Statements (Delphi)") of threadvar#Thread-local_Variables "Variables (Delphi)")
destructor "Classes and Objects (Delphi)") if#If_Statements "Declarations and Statements (Delphi)") or#Logical_.28Bitwise.29_Operators "Expressions (Delphi)") to#For_Statements "Declarations and Statements (Delphi)")
dispinterface "Automation Objects (Win32 Only)") implementation#The_Implementation_Section "Programs and Units (Delphi)") packed "Structured Types (Delphi)") try "Exceptions (Delphi)")
div#Arithmetic_Operators "Expressions (Delphi)") in procedure "Procedures and Functions (Delphi)") type#Enumerated_Types "Simple Types (Delphi)")
do#For_Statements "Declarations and Statements (Delphi)") inherited#Inherited "Methods (Delphi)") program "Programs and Units (Delphi)") unit "Programs and Units (Delphi)")
downto#For_Statements "Declarations and Statements (Delphi)") initialization#The_Initialization_Section "Programs and Units (Delphi)") property "Properties (Delphi)") until#Repeat_Statements "Declarations and Statements (Delphi)")
else#If_Statements "Declarations and Statements (Delphi)") inline raise#Raising_and_Handling_Exceptions "Exceptions (Delphi)") uses#Unit_References_and_the_Uses_Clause "Programs and Units (Delphi)")

说明:
(1)单词at和on也具有特殊的含义,不要定义与它们同名的标识符。
(2)Object Pascal是不区分大小写的,因此,保留字也不区分大小写,例如,And、AND和and等都被看作同样的保留字。
(3)在Delphi集成开发环境的代码编辑器中,将以黑体显示保留字和指令字。在定义标识符时不要与这些以黑体字显示的单词一样。

指令符也是具有特定意义的单词。但是,它们与保留字的不同之处是:指令字只在特殊的程序位置、或当上下文关联时有意义的程序区段有自己特殊的意义,而在其他场合,用户可以对其重新定义或用作其他用途。即可以将某个指令符定义为标识符,Object Pascal 不会指示出错,当用户重新定义了这些指令字后,在作用域内它们就失去了原来的意义了,这容易带来混淆,用户应尽可能少用。
Object Pascal中规定的指令符如下所示。

absolute#Absolute_Addresses "Variables (Delphi)") export****12 name public#Visibility_of_Class_Members "Classes and Objects (Delphi)") stdcall#Calling_Conventions "Procedures and Functions (Delphi)")
abstract#Abstract_Methods "Methods (Delphi)") external#External_Declarations "Procedures and Functions (Delphi)") near****1 published#Visibility_of_Class_Members "Classes and Objects (Delphi)") strict#Strict_Visibility_Specifiers "Classes and Objects (Delphi)")
assembler****12 far****1 nodefault "Properties (Delphi)") read "Properties (Delphi)") stored#Storage_Specifiers "Properties (Delphi)")
automated#Automated_Members_.28Win32_Only.29 "Classes and Objects (Delphi)") final#Final_Methods "Methods (Delphi)") operator "Operator Overloading (Delphi)")****10 readonly#Dispatch_interface_properties "Automation Objects (Win32 Only)") unsafe
cdecl#Calling_Conventions "Procedures and Functions (Delphi)") forward "Procedures and Functions (Delphi)") out#Out_Parameters "Parameters (Delphi)") reference****9 varargs#External_Declarations "Procedures and Functions (Delphi)")
contains#The_contains_clause "Packages (Delphi)")****7 helper "Class and Record Helpers (Delphi)")****8 overload#Overloading_Methods "Methods (Delphi)") register#Calling_Conventions "Procedures and Functions (Delphi)") virtual#Virtual_and_Dynamic_Methods "Methods (Delphi)")
default "Properties (Delphi)") implements override reintroduce#Reintroduce "Methods (Delphi)") winapi#Calling_Conventions "Procedures and Functions (Delphi)")****6
delayed#Delayed_Loading "Libraries and Packages (Delphi)") index#Index_Specifiers "Properties (Delphi)") package "Packages (Delphi)")****7 requires#The_requires_clause "Packages (Delphi)")****7 write "Properties (Delphi)")
deprecated#Hinting_Directives "Declarations and Statements (Delphi)")****11 inline****2 pascal#Calling_Conventions "Procedures and Functions (Delphi)") resident****1 writeonly#Dispatch_interface_properties "Automation Objects (Win32 Only)")
dispid "Automation Objects (Win32 Only)") library****311 platform#Hinting_Directives "Declarations and Statements (Delphi)")****11 safecall#Calling_Conventions "Procedures and Functions (Delphi)")
dynamic local****4 private#Visibility_of_Class_Members "Classes and Objects (Delphi)") sealed#Class_Types "Classes and Objects (Delphi)")****5
experimental#Hinting_Directives "Declarations and Statements (Delphi)")****11 message#Message_Methods "Methods (Delphi)") protected#Visibility_of_Class_Members "Classes and Objects (Delphi)") static#Class_Static_Methods "Methods (Delphi)")

说明:指令符private、protected、public、published和automated在定义对象类型时也为保留字,而在其他场合则作为指令符。

Note:

  1. far, near, and resident are obsolete.
  2. inline is used directive-style at the end of procedure and function declaration to mark the procedure or function for inlining , but became a reserved word for Turbo Pascal.
  3. library is also a keyword when used as the first token in project source code; it indicates a DLL target. Otherwise, it marks a symbol so that it produces a library warning when used.
  4. local was a Kylix directive and is ignored for Delphi for Win32.
  5. sealed is a class directive with odd syntax: ‘class sealed’. A sealed class cannot be extended or derived (like final in C++).
  6. winapi defines the default platform calling convention. For example, on Win32 winapi is the same as stdcall.
  7. package, when used as the first token, indicates a package target and enables package syntax. requires and contains are directives only in package syntax.
  8. helper indicates "class helper for".
  9. reference indicates a reference to a function or procedure.
  10. operator indicates class operator.
  11. platform, deprecated, experimental, and library are hinting (or warning) directives. These directives produce warnings at compile time.
  12. assembler and export directives have no meaning. They exist only for the backward compatibility.

注释

注释可增加程序的可读性和可维护性。Object Pascal 语言中的注释有下面三种形式。
(1)组合符号{与}的成对使用表示它们之间的内容为注释部分。
(2)组合符号(*与*)的成对使用表示它们之间的内容为注释部分。
(3)符号//的单个使用表示所在行的该符号之后的内容为注释。
说明:
(1)注释符{与}、(*与*)在使用时不支持注释的嵌套,而且必须成对使用。
(2)建议对于单行和少量几行的注释使用符号//,对于大块注释使用{和}或(*和*)。
(3)有时可以利用注释在代码中形成一个醒目的标志。比如利用注释符号形成一个矩形方框,在其中可以添加一些重要的说明文字。
(4)在注释符{或(*后紧接着是一个美元符号$时,表示该句是一个编译器指令,它与普通的注释不同,通常用来对编译过程进行设置。

由于Delphi集成开发环境中的代码编辑器在显示不同类型的代码时通过使用不同的颜色来加以区别,所以在编程过程中,只要注意文件中代码的颜色,一般就不会错误地使用注释符了。
注释可以放在程序的任何地方,例如:

begin
    Form1.Caption:='欢迎使用Object Pascal 语言';{将Form1标题改为"欢迎使用Object Pascal语言"}
    Form1.Width:=300;                   (*将Form1的宽度改为300*)
    Form1.Height:=250;                  //将Form1的高度改为250
End;

数据类型

Object Pascal是一种强类型的语言,即它对数据类型的定义、声明以及数据赋值和传递操作等都制定有严格的语法规则。因此,掌握好数据类型的概念是设计好程序的关键。

Object Pascal语言支持的数据类型比较丰富,提供了多种数据类型,。数据类型可以分为标准数据类型及高级数据类型等,还可以通过数据类型声明语句在预定义数据 类型的基础上定义新的数据类型。

Object Pascal的数据类型

类型 名称 说明
整型 Integer 标准数据类型
实型 Real 标准数据类型
字符型 Character 标准数据类型
字符串型 String 标准数据类型
布尔型 Boolean 标准数据类型
枚举型 Enumerated 高级数据类型
子界型 Subrange 高级数据类型
集合类型 Set 高级数据类型
数据类型 Array 高级数据类型
记录类型 Record 高级数据类型
文件类型 File 高级数据类型
类类型 Class 高级数据类型
类引用类型 Class Reference 高级数据类型
接口类型 Interface 高级数据类型
指针类型 Pointer 高级数据类型
过程类型 Procedural 高级数据类型
可变类型 Variant 高级数据类型

(1)标准数据类型属于Object Pascal内部约定的数据类型,无须定义就可以直接使用。
(2)高级数据类型体现了特殊的数据结构,在使用之前必须由用户自己定义。
(3)数据类型中的整型、字符型、布尔型、枚举型和子界型被称为顺序类型,其取值是一个有序的集合,每一个可能的取值都与顺序(整数值)有关,即其取值与某一整数相对应。

数值型数据

Object Pascal中的数值型数据可以分为整数类型和实数类型。

整数类型是存储整数数据的类型:

Object Pascal的整型数据

类型 ** 名称** ** 字长** ** 取值范围**
短整型 Shortint 有符号8位 -128~127
小整型 Smallint 有符号16位 -32768~32767
长整型 Longint 有符号32位 -2147483648~214748647
字节型 Byte 无符号8位 0~255
字型 Word 无符号16位 0~65535
长字型 Longword 无符号32位 0~429967295
整型 Integer 有符号32位 -2147483648..2147483647(-2^31..2^31-1)
64位整型 Int64 有符号64位 -9223372036854775808..9223372036854775807(-2^63..2^63-1)
序数型 Cardinal 无符号32位 0..4294967295(0..2^32-1)
序数型 UInt64 无符号64位 0..18446744073709551615(0..2^64-1)

实数类型是存储实数数据的类型,Object Pascal语言中的实数数据类型:

表2-3 Object Pascal 语言中的实数数据类型

Type Platform Approximate Positive Range Significant decimal digits Size in bytes
Real48 all 2.94e-39 .. 1.70e+38 11-12 6
Single all 1.18e-38 .. 3.40e+38 7-8 4
Double all 2.23e-308 .. 1.79e+308 15-16 8
Real all 2.23e-308 .. 1.79e+308 15-16 8
Extended 32-bit Intel Windows 3.37e-4932 .. 1.18e+4932 10-20 10
Extended 64-bit Intel Linux 3.37e-4932 .. 1.18e+4932 10-20 16
Extended 64-bit Intel macOS 3.37e-4932 .. 1.18e+4932 10-20 16
Extended 64-bit Intel Windows 2.23e-308 .. 1.79e+308 15-16 8
Extended ARM platforms (32-bit Android, 64-bit Android, 64-bit iOS, 64-bit macOS) 2.23e-308 .. 1.79e+308 15-16 8
Comp all -9223372036854775808.. 9223372036854775807
(-2^63.. 2^63-1)
10-20 8
Currency all -922337203685477.5808.. 922337203685477.5807(-(263+1)/10000.. 263/10000) 10-20 8

说明:
(1)前4种为基本实型,Real为一般实型。
(2)Real类型与Double类型完全等价。
(3)Extended类型比Real类型的精度更高,但与其他语言或操作平台的兼容性较差,因此应尽量避免使用。
(4)Currency类型是专为处理货币值而设计的,该类型至少有4位有效的小数位。当与其他实型数据进行混合运算时,Delphi自动将运算结果执行除10000的操作而转换成Currency类型。

  • Real is equivalent to Double, in the current implementation.
  • Real48 is maintained for backward compatibility. Since its storage format is not native to the Intel processor architecture, it results in slower performance than other floating-point types.
  • The 6-byte Real48 type was called Real in earlier versions of Object Pascal. If you are recompiling code that uses the older, 6-byte Real type in Delphi, you may want to change it to Real48. You can also use the {$REALCOMPATIBILITY ON} compiler directive to turn Real back into the 6-byte type.
  • Extended offers greater precision on 32-bit platforms than other real types.
  • On 64-bit Windows and all ARM platforms Extended is an alias for a Double; that is, the size of the Extended data type is 8 bytes. Thus you have less precision using an Extended on these platforms compared to 32-bit platforms, where Extended is 10 bytes. Therefore, if your applications use the Extended data type and you rely on precision for floating-point operations, this size difference might affect your data. Be careful using Extended if you are creating data files to share across platforms. For more information, see The Extended Data Type Is 2 Bytes Smaller on 64-bit Windows Systems.
  • The Comp (computational) type is native to the Intel processor architecture and represents a 64-bit integer. It is classified as a real, however, because it does not behave like an ordinal type. (For example, you cannot increment or decrement a Comp value.) Comp is maintained for backward compatibility only. Use the Int64 type for better performance.
  • Currency is a fixed-point data type that minimizes rounding errors in monetary calculations. It is stored as a scaled 64-bit integer with the 4 least significant digits implicitly representing decimal places. When mixed with other real types in assignments and expressions, Currency values are automatically divided or multiplied by 10000.

字符型数据

Object Pascal中的字符型数据可以分为字符型和字符串型两类7种。

字符型是存储单个字符数据的类型,Object Pascal包括三种形式的字符型数据.

类型 名称 字节数 取值范围
Ansi字符型 AnsiChar 1 扩展ANSI字符集
宽字符型 WideChar 2 Unicode字符集
字符型 Char 1 扩展ANSI字符集

说明:
(1)前两种为基本字符类型,后一种为一般字符类型。
(2)Char类型与AnsiChar类型完全等价。
最常用的字符类型是Char类型。

正文完
 0