Delphi常用关键字用法详解

本文详细介绍了Delphi中常用的各个关键字名称及用法,供大家在编程过程中借鉴参考之用。详情如下:

absolute:

[delphi]  view plain  copy
  1. //它使得你能够创建一个新变量, 并且该变量的起始地址与另一个变量相同.  
  2. var  
  3.  Str: string[32];  
  4.  StrLen: Byte absoluteStr;  
  5. //这个声明指定了变量StrLen起始地址与Str相同.  
  6. //由于字符串的第0个位置保存了字符串的长度, 所以StrLen的值即字符串长度.  
  7. begin  
  8. Str := 'abc';  
  9. Edit1.Text := IntToStr(StrLen);  
  10. end;  

abstract:

[delphi]  view plain  copy
  1. //它允许你创建抽象的方法, 包括有抽象方法的类称为抽象类.  
  2. //Abstract关键字必须与Virtual或Dynamic关键字同时使用, 因为抽象方法必须被覆盖式实现.  
  3. //抽象类不能实例化, 抽象方法不能包含方法体.  
  4. type  
  5.  TDemo = class  
  6.   private  
  7.   protected  
  8.    procedure X; virtual; abstract;  
  9.   public  
  10.    constructor Create;  
  11.    destructor Destroy; override;  
  12.   published  
  13.  end;  

and:

[delphi]  view plain  copy
  1. //一、表示逻辑与  
  2. if (a>0and (b>0then  
  3. //二、表示位运算  
  4. var  
  5. a,b,c: Integer;  
  6. begin  
  7. c := (a and b);  
  8. end;  
  9. //使用And表示逻辑时, And左右的表达式必须用小括号括起, 以避免以生条件的冲突.  
  10. //例如:  
  11. if a>0 and b>0 then  
  12. //编译器可能会理解为:  
  13. if a>(0 and b)>0 then  
  14. //或:  
  15. if (a>0and (b>0then  
  16. //但是实际编译时, 编译器会产生一个冲突, 报告错误.  
  17. //并且第一种可能包含了a>b>c的形式, 这在Delphi中不被支持.  
  18. //所以使用And运算符时必须使用括号, 以区分左右的条件.  
  19. //表示位运算时也必须加上括号, 将And以及左右参数括起.  

array:

[delphi]  view plain  copy
  1. //Array用于表示数组, 任何的对象都能被声明成数组.数组分为静态和动态的2种.  
  2. //静态数组  
  3. var  
  4. Arr1: array [1..10of Integer;  
  5.   
  6. //动态数组, 由于声明时不知其元素个数, 所以必须在后期用SetLength方法设置数组的大小  
  7. var  
  8. Arr2: array of Integer;  
  9.   
  10. //数组作为参数时, 不能传入数组的大小, 只能传入数组名, 然后用Length方法获取数组的元素个数  
  11. function X(A: array of Integer): Integer;  
  12. var  
  13. i: Integer;  
  14. begin  
  15. Result := 0;  
  16. for i := 0 to Length(A)-1 do  
  17. Result := Result + A[i];  
  18. end;  

as:

[delphi]  view plain  copy
  1. //As用于将一个对象转换为另一个对象  
  2. procedure BtnClick(Sender:TObject);  
  3. begin  
  4.  (Sender as TButton).Caption := 'Clicked';  
  5. end;  
  6. //对于对象填充接口的转换, 必须用As进行  
  7. (HTTPRIO as IExp).GetConnection;  
  8.   
  9. //As不能用于数据类型的转换, 下面的代码是错误的:  
  10. var  
  11. i: Integer;  
  12. s: string;  
  13. begin  
  14. s := (i as string);  
  15. end;  
  16. //正确写法是:  
  17. s := string(i);  

asm:

[delphi]  view plain  copy
  1. //Asm关键字用于插入汇编代码, 使用汇编代码时, 必须使用asm...end;的结构, 而非begin...end;  
  2. function IntToHex(Value: Integer; Digits: Integer): string;  
  3. asm  
  4.  CMP EDX, 32  
  5.  JBE @A1  
  6.  xor EDX, EDX  
  7.  @A1: PUSH ESI  
  8.  MOV ESI, ESP  
  9.  SUB ESP, 32  
  10.  PUSH ECX  
  11.  MOV ECX, 16  
  12.  CALL CvtInt  
  13.  MOV EDX, ESI  
  14.  POP EAX  
  15.  CALL System.@LStrFromPCharLen  
  16.  ADD ESP, 32  
  17.  POP ESI  
  18. end;  

assembler:

[delphi]  view plain  copy
  1. //Assembler关键字用于支持早期的汇编, 如80386等.  
  2. //它和Asm的区别:Asm允许使用Win32汇编, 而Assembler只允许80x86汇编, 它不允许Invoke语句的出现.  
  3. function IntToHex(AValue: Int64): string; assembler;  

automated:

[delphi]  view plain  copy
  1. //Automated访问区分符用于描述一个自动类型的成员, 它能够使程序的版本向下兼容.  
  2. //ComObj单元内的成员及其实例不能使用Automated访问区分符.  
  3. type  
  4.  TDemo = class  
  5.   automated  
  6.    Str:WideString;  
  7.  end;  
  8. //在程序的下一个版本中, 将Str做了修改, 变成  
  9. type  
  10. TDemo = class  
  11. automated  
  12. Str: AnsiString;  
  13. end  
  14. //则新版本的Str变量能够接受旧版本的WideString型数据, 并自动转换成AnsiString.  
  15. //在实际开发中, 如果没有特殊的需要, 一般不用automated访问区分符.  

begin:

[delphi]  view plain  copy
  1. //begin关键字用于表示一段程序或一个结构的开始, 必须用end关键字来结束.  
  2. procedure X;  
  3. begin  
  4.  ShowMessage('A Demo');  
  5. end;  
  6. //一般的结构, 如If, For, While等也需要用begin关键字来标出结构起始点  
  7. for i:=1 to 100 do  
  8. begin  
  9. sum := sum + i;  
  10. if sum > 1000 then Break;  
  11. end;  

case:

[delphi]  view plain  copy
  1. //Case语句用于完成条件选择, Case语句的的被选择对象必须是有序类型, 包括整型, 枚举类型, 字符型等.  
  2. //Case语句必须由end结束,如果没有相符合的选择项, 可以加入else来作出通用选择.  
  3. function GetDays(AYear,AMonth: Integer): Integer;  
  4. begin  
  5.  case AMonth of  
  6.   1,3,5,7,8,10,12: Result := 31;  
  7.   4,6,9,11: Result := 30;  
  8.   2begin  
  9.   if IsLeapYear(AYear) then  
  10.    Result:=29  
  11.   else  
  12.    Result:=28;  
  13.   end;  
  14.  else  
  15.   Result:=0;  
  16. end;  

cdecl:

[delphi]  view plain  copy
  1. //Cdecl是函数调用协定的一种, 它规定了从C或C++编写的DLL中调用函数所必须遵守的规则.  
  2. //它可以将C或C++中的数据类型转换为Delphi的.  
  3. //例如C++中的代码:  
  4. int X(int i)  
  5. { 
  6.  return i*2; 
  7. }  
  8. //这个函数被编译在Demo.dll中, 用Delphi调用时必须使用:  
  9. function X(i: Integer): Integer; Cdecl; external 'Demo.dll';  

class:

[delphi]  view plain  copy
  1. //Class关键字用于声明或继承一个类, 也可以使类和接口同时继承.  
  2. //另外, Class关键字也能用于声明类通用方法, 使得父类可以从类内访问子类的方法.  
  3. type  
  4.  ClassDemo = class(TObject)  
  5.   private  
  6.   public  
  7.    constructor Create;  
  8.  end;  
  9. //如果用class声明方法, 则该方法在类与相关类中都可以使用, 譬如:  
  10. type  
  11. ClassA = class  
  12. private  
  13. public  
  14. procedure Y;  
  15. end;  
  16.   
  17. type  
  18. ClassB = class(ClassA)  
  19. private  
  20. public  
  21. class procedure X;  
  22. end;  
  23. //则在使用时ClassA能够直接访问ClassB的X方法  
  24. procedure ClassA.Y;  
  25. begin  
  26. Self.X;  
  27. end;  
  28. //此时父类将子类的class方法作为自身的方法进行调用.  

const:

[delphi]  view plain  copy
  1. //Const关键字用于声明常量, 使用const声明的数据将不能在程序中被改变.  
  2. //也可以用来声明函数参数, 用const指定的参数不允许在函数中改变.  
  3. const MyFileName = 'Delphi';  
  4. const MyInteger = 100;  
  5. //用Const声明常量不需要指出其数据类型, 系统会自动判断类型, 并作自动调整.  
  6. //函数中可以用const声明不可更改的参数  
  7. function X(const i: Integer): string;  
  8. //此时在函数操作过程中, i的值不可改变.  

constructor:

[delphi]  view plain  copy
  1. //constructor关键字用来声明一个类的构造函数, 当类被实例化时, 首先调用此函数  
  2. //构造函数一般用Create表示, Create方法能够连带类中存在的CreateWnd方法.  
  3. type  
  4.  ClassDemo = class(TObject)  
  5.   private  
  6.    fValue: Integer;  
  7.   public  
  8.    constructor Create;  
  9.  end;  
  10. constructor ClassDemo.Create;  
  11. begin  
  12. fValue := 0;  
  13. end;  

contains:

[delphi]  view plain  copy
  1. //Contains关键字指出了某个包(Package)是否包含某个文件.  
  2. //用Contains引入的文件必须被添加到包文件中, 它可以避免关键文件的引用丢失.  
  3. package DATAX;  
  4.  requires  
  5.   rtl, clx;  
  6.  contains  
  7.   Db, DBLocal, DBXpress;  
  8. end.  

default:

[delphi]  view plain  copy
  1. //Default关键字用于指出一个属性的默认值  
  2. //只有有序类型的属性才允许默认值的存在, 否则必须在构造函数中初始化属性值.  
  3. type  
  4.  ClassDemo = class  
  5.   private  
  6.    fValue: Integer;  
  7.   published  
  8.    property Value: Integer read fValue write fValue default 0;  
  9.  end;  
  10. //它也可以指出一个类的默认属性  
  11. property strings[Index: Integer]: string read GetString write PutString; Default;  

destructor:

[delphi]  view plain  copy
  1. //Destructor用于标识析构函数, 析构函数在类被释放时自动调用.  
  2. //析构函数只允许覆盖, 再不允许重载.析构函数通常用Destroy作为函数名.  
  3. type  
  4.  ClassDemo = class(TComponent)  
  5.   public  
  6.    destructor Destroy;override;  
  7.  end;  
  8. //由于TComponent类中也有Destroy方法, 所以要将其重写  
  9. //但是若要重载析构函数, 则不允许, 下面代码是错误的:  
  10. destructor Destroy; overload;  

dispid:

[delphi]  view plain  copy
  1. //DispId关键字被用在DispInterface接口中, 用于指定特定的适配序号.  
  2. //在DispInterface接口中, 适配序号必须是唯一的,   
  3. //如果不指定DispId, 则系统会自动分配适配序号给接口内每一个方法.  
  4. //可以通过适配序号访问DispInterface接口中的方法.  
  5. type  
  6.  IStringsDisp = dispinterface  
  7.   ['{EE05DFE2-5549-11D0-9EA9-0020AF3D82DA}']  
  8.   property ControlDefault[Index: Integer]: Olevariant dispid 0; default;  
  9.   function Count: Integer; dispid 1;  
  10.   property Item[Index: Integer]: Olevariant dispid 2;  
  11.   procedure Remove(Index: Integer); dispid 3;  
  12.   procedure Clear; dispid 4;  
  13.   function Add(Item: Olevariant): Integer; dispid 5;  
  14.   function _NewEnum: IUnknown; dispid -4;  
  15.  end;  

dispinterface:

[delphi]  view plain  copy
  1. //DispInterface用于声明一个特定的适配器接口, 这个适配器能够接受标准系统接口中传入传出的数据.  
  2. //用DispInterface声明的接口不能被继承, 只能够被引用.  
  3. //DispInterface中方法只能调用, 并且必须被动态绑定.  
  4. //可以通过DispId为接口内方汉分配适配序号.  
  5. //DispInterface仅能用于Windows平台, 如果在Linux下进行开发, 则此关键字会自动被系统屏蔽.  
  6. //通常情况下, 不使用DispInterface.  
  7. //实例请参见DispId  

div:

[delphi]  view plain  copy
  1. //Div用于求两数之整数商.用于Div运算的两个数值必须均为整型, 其运算结果也为整型.  
  2. var  
  3.  a,b,c:Integer;  
  4. begin  
  5.  a := 20; b := 3;  
  6.  c := a div b; {6}  
  7. end;  

do:

[delphi]  view plain  copy
  1. //Do关键字用于For, While, On, With语句, 构成特定的结构  
  2. //For语句:  
  3. for i := 1 to 100 do sum:=sum+i;  
  4.   
  5. //While语句:  
  6. while i < 100 do  
  7. begin  
  8.  sum := sum + i;  
  9.  Inc(i);  
  10. end;  
  11.   
  12. //On语句(异常处理):  
  13. try  
  14.  i := StrToInt(s);  
  15. except  
  16.  on exception do ShowMessage('Error!');  
  17. end;  
  18.   
  19. //With语句:  
  20. with Memo1.Lines do  
  21. begin  
  22.  Clear;  
  23.  Append('abc');  
  24.  Append('123');  
  25. end;  

downto:

[delphi]  view plain  copy
  1. //DownTo关键字用于For语句, 指明循环变量是递减的.  
  2. for i := 100 downto 1 do  
  3. ListBox1.Items.Add(IntToStr(i));  
  4. //在For语句中, 循环变量递增用To关键字, 递减用DownTo关键字.  

dynamic:

[delphi]  view plain  copy
  1. //Dynamic用于声明一个动态的方法,   
  2. //动态方法可以被覆盖, 并且可以使代码大小尽可能的减少(区别于Virtual).  
  3. procedure X(i: Integer); dynamic;  

else:

[delphi]  view plain  copy
  1. //else用于引导程序的运行方向, 它可以与If, Case和On语句联用, 当条件不满足时, 转到else下运行  
  2. //If语句(在If语句中, else前不允许有分号):  
  3. if a > b then  
  4. c := a  
  5. else  
  6. c:=b;  
  7.   
  8. //Case语句:  
  9. case Tag Of  
  10. 1:Result:=1;  
  11. 2:Result:=2;  
  12. 3:Result:=3;  
  13. else  
  14. Result:=0;  
  15. end;  
  16.   
  17. //On语句(异常处理):  
  18. try  
  19. i := StrToInt(s);  
  20. Excpet  
  21. on EZeroDivide do Result := 1;  
  22. on EOverflow do Result := 2;  
  23. else  
  24. Result := 0;  
  25. end;  

end:

[delphi]  view plain  copy
  1. //End用于结束一个语句块或是一个单元.  
  2. //它可以与begin, Case, Class, Interface, Asm, Unit, Package等相匹配.  
  3. //对于语句块(局部结束), End后必须添加分号.  
  4. //而对于单元或包(全局结束), end后必须添加句号.  
  5. //在If语句中else关键字前的End后不允许添加符号.  
  6. procedure X;  
  7. begin  
  8.  with Button1 do  
  9.  begin  
  10.   if Button1.ShowHint then  
  11.    Button1.Caption := 'Hinted'  
  12.   else  
  13.    Button1.Caption := 'Not Hinted';  
  14.  end;  
  15. end;  
  16. //在包内使用End来结束:  
  17. package DATAX;  
  18. requires  
  19. rtl,  
  20. clx;  
  21. contains Db, DBLocal, DBXpress;  
  22. end.  

except:

[delphi]  view plain  copy
  1. //except关键字用于异常处理, 必须用在try语句内, 如果发生异常, 则执行except后的语句  
  2. try  
  3.  i := StrToInt(s);  
  4. except  
  5.  ShowMessage('Error!');  
  6. end;  

export:

[delphi]  view plain  copy
  1. //Export标明了函数调用协定, 指出函数可以被输出, 输出的函数能被本地或远程调用.  
  2. //其他程序可以用dll的形式调用程序内的函数.它是向下兼容的.  
  3. function Add(a,b: Integer): Integer; export;  
  4. //如果这个程序被编译为Demo.exe, 并且另一个程序需要调用这个函数, 可以使用以下语句  
  5. function Add(a,b: Integer): Integer; stdcall; external 'Demo.exe';  

exports:

[delphi]  view plain  copy
  1. //exports用于输出对象, 它必须被用在接口和实现之间, 可以同时输出多个项, 项与项之间用逗号分开.  
  2. libraryDemo;  
  3. function X(i: Integer): string; stdcall;  
  4. begin  
  5.  Result:=IntToStr(i);  
  6. end;  
  7.   
  8. exports  
  9.  X;  
  10.   
  11. begin  
  12. end.  
  13.   
  14. //如果输出的对象被重载, 则必须给对象起个别名, 并注明参数.  
  15. library Demo;  
  16.   
  17. function X(i: Integer): string; overload; stdcall;  
  18. begin  
  19.  Result := IntToStr(i);  
  20. end;  
  21.   
  22. function X(s: string): Integer; overload; stdcall;  
  23. begin  
  24.  Result := StrToInt(s);  
  25. end;  
  26.   
  27. exports  
  28. X(i: Integer) name 'x1',  
  29. X(s: string) name 'x2';  
  30.   
  31. begin  
  32. end.  

external:

[delphi]  view plain  copy
  1. //External关键字用于引用一个外部的或是OBJ内的方法.  
  2. {$L Demo.OBJ}  
  3. procedure X(i:Integer);external;  
  4. //如果是从dll或外部程序中引用, 则可以使用以下代码:  
  5. function A(FileName: string): string; external 'Demo.dll';  
  6.   
  7. //如果被引用的函数被重载, 则必须另外指出引用的名称.  
  8. function A(Name: string): string; overload; stdcall; external 'Demo.dll' name 'A1';  
  9. function A(Code: Integer): string; overload; stdcall; external 'Demo.dll' name 'A2';  
  10.   
  11. //使用External关键字时, 必须注意大小写, 否则将出现错误.  

far:

[delphi]  view plain  copy
  1. //Far标明了函数调用协定, 指出函数可以被远程调用.  
  2. //其他程序可以用dll的形式调用程序内的函数.它是向下兼容的.  
  3. functionAdd(a,b: Integer): Integer; Far;  
  4. //如果这个程序被编译为Demo.exe, 并且另一个处于其他计算机的程序需要调用这个函数, 可以使用以下语句:  
  5. function Add(a,b: Integer): Integer; stdcall; external 'Demo.exe';  

file:

[delphi]  view plain  copy
  1. //File关键字指出了文件操作类型, 文件必须被声明为File,   
  2. //如果在File后追加Of和文件类型, 则文件可以被定义为读写指定类型数据.  
  3. type  
  4.  TPerson = record  
  5.   PName: string[32];  
  6.   PAge: Integer;  
  7.  end;  
  8. var  
  9.  PFile: file of TPerson;  

finalization:

[delphi]  view plain  copy
  1. //finalization关键字标识了单元被释放时所要调用的方法,   
  2. //通常是释放掉单元中不能自动释放的对象, 也可以不用.  
  3. //finalization最常用的情况是对OLE对象做反初始化.  
  4. initialization  
  5.  ActiveX.OleInitialize(nil);  
  6. finalization  
  7.  ActiveX.OleUninitialize;  

finally:

[delphi]  view plain  copy
  1. //finally关键字指出了异常处理中最后必须要调用的方法,   
  2. //不论是否发生异常, finally后的语句总是在try语句结束时执行.  
  3. try  
  4.  Node := Node.GetNext;  
  5.  Edit1.Text := Node.Text;  
  6. finally  
  7.  Node := nil;  
  8. end;  

for:

[delphi]  view plain  copy
  1. //For关键字引出For循环结构, 用于做指定次数的循环.  
  2. for i := 1 to 100 dosum := sum + i;  
  3. //如果循环变量是递减的, 则可以用DownTo关键字  
  4. for i := 100 downto 1 do Inc(sum);  

forward:

[delphi]  view plain  copy
  1. //Forward关键字用于方法的前置定义.只定义方法声明, 然后在程序的后面对方法进行实现.  
  2. //这么做有利于代码的可读性, 可以将所有的声明放在一起, 然后将所有的实现也放在一起.  
  3. function X(i: Integer): Integer; forward;  
  4. procedure Y(s: string); forward;  
  5. ...  
  6. function X;  
  7. begin  
  8.  Result := i * 2;  
  9. end;  
  10. procedure Y;  
  11. begin  
  12. WriteLn(s);  
  13. end;  
  14.   
  15. //用Forward前置声明的方法在实现时不需要再输入方法的参数和返回值, 直接使用方法名即可.  

function:

[delphi]  view plain  copy
  1. //Function用于声明函数  
  2. functionX(i: Integer): Integer;  
  3. //它也可以用于动态函数的声明  
  4. type  
  5.  TFun = function(i: Integer): Integer of object;  
  6.   
  7. //动态声明时, 不需要指出函数名, 只需要指出参数和返回类型就可以, 具体的函数名可以在后期绑定.  

goto:

[delphi]  view plain  copy
  1. //Goto语句用在跳转行号, 可以跳转到当前结构层内任意位置.  
  2. //必须在声明处用label关键字声明行号.  
  3. //由于Goto语句会破坏程序的结构, 不推荐使用.  
  4. var  
  5.  a,b: Integer;  
  6. label  
  7.  X,Y;  
  8. begin  
  9.  if a > b then  
  10.   goto X  
  11.  else  
  12.   goto Y;  
  13. X:  
  14.  WriteLn('a > b');  
  15. Y:  
  16.  WriteLn('b > a');  
  17. end;  

if:

[delphi]  view plain  copy
  1. //If关键字引出If条件语句, 用于对条件进行判断.  
  2. var  
  3.  a,b: Integer;  
  4. begin  
  5.  a := 2; b := 3;  
  6.  if a>b then  
  7.   WriteLn('a=' + IntToStr(a))  
  8.  else  
  9.   WriteLn('b=' + IntToStr(b));  
  10. end;  
  11. //If语句的通常结构是If...Then...else, else语句也可以不要.  
  12. //在If语句内如果有多个子语句, 则必须用begin...End结构进行区分.  
  13. if a > b then  
  14. begin  
  15.  WriteLn('a>b');  
  16.  WriteLn('a=' + IntToStr(a));  
  17.  WriteLn('b=' + IntToStr(b));  
  18. End  
  19. else  
  20.  WriteLn('b>a');  

implementation:

[delphi]  view plain  copy
  1. //Implementation标识了单元中的实现部分, 单元的基本结构为:  
  2. //Unit...Interface...implementation...end.  
  3. //函数体, 过程体等必须写在implementation关键字后.  
  4. //如果在implementation后引用对象, 则对象是非公开的, 仅能供单元自身使用.  
  5. implementation  
  6.  uses frmAbout;  
  7. begin  
  8.  FormAbout.Show;  
  9. end;  
  10. //一个完整的单元必须拥有implementation部分.  

implements:

[delphi]  view plain  copy
  1. //Implements指出了一个属性从接口继承, 此时属性被转换成接口对象.  
  2. //通过接口动态绑定属性, 并动态的设定属性值.  
  3. type  
  4.  IMyInterface = interface  
  5.   procedure P1;  
  6.   procedure P2;  
  7.  end;  
  8.  TMyImplclass = class  
  9.   procedure P1;  
  10.   procedure P2;  
  11.  end;  
  12.  TMyclass = class(TInterfacedObject, IMyInterface)  
  13.   FMyImplClass: TMyImplClass;  
  14.   property MyImplClass: TMyImplclass read FMyImplclass implements IMyInterface;  
  15.   procedure IMyInterface.P1 = MyP1;  
  16.   procedure MyP1;  
  17.  end;  
  18. //通过implements声明后, 可以在类声明时指出接口中方法的实体, 如上例中的:  
  19. procedure IMyInterface.P1 = MyP1;  

in:

[delphi]  view plain  copy
  1. //In用于判断一个集合中是否包含某个元素.被判断的内容必须是单个集合元素和一个集合的实例.  
  2. type  
  3.  TCol = (cA,cB,cC);  
  4.  TCols = set of TCol;  
  5. var  
  6.  Cols: TCols;  
  7. begin  
  8.  Cols := [cA,cB];  
  9.  if cA in Cols then  
  10.   ShowMessage('cA in Cols')  
  11.  else  
  12.   ShowMessage('cA not in Cols');  
  13. end;  
  14. //In也用于工程文件中, 用于标识某个文件是否被工程所引用.  
  15. Uses  
  16.  Unit1 in 'Unit1.pas';  
  17.   
  18. //In可以被用在For语句中, 用于循环取出一个集合中的元素.  
  19. var  
  20.  s: string;  
  21.  sl: TStringList;  
  22. begin  
  23.  ...  
  24.  for s In sl do  
  25.  begin  
  26.   ShowMessage(s);  
  27.  end;  
  28. end;  

index:

[delphi]  view plain  copy
  1. //Index用于在属性中标识序号, 以便用相同的属性方法(Get,Set)对不同的属性进行操作.  
  2. type  
  3.  TForm1 = class(TForm)  
  4.  private  
  5.   function GetInfo(const Index: Integer): Longint;  
  6.   procedure SetInfo(const Index: Integer; const Value: Longint);  
  7.  public  
  8.   property iLeft:Longint index 0 read GetInfo write SetInfo;  
  9.   property iTop:Longint index 1 read GetInfo write SetInfo;  
  10.   property iWidth:Longint index 2 read GetInfo write SetInfo;  
  11.   property iHeight:Longint index 3 read GetInfo write SetInfo;  
  12.  end;  
  13. function TForm1.GetInfo(const Index: Integer): Longint;  
  14. begin  
  15.  case Index of  
  16.   0: result := self.Left;  
  17.   1: Result := self.Top;  
  18.   2: result := self.Width;  
  19.   3: result := self.Height;  
  20.  end;  
  21. end;  
  22.   
  23. //Index关键字也用于在属性中指出多个元素, 例如:  
  24. property Selected[Index: Integer]: Boolean read GetSelected write SetSelected;  

inherited:

[delphi]  view plain  copy
  1. //Inherited用于调用父类的方法.  
  2. type  
  3.  TDemo = class(TComponent)  
  4.  public  
  5.   constructor Create(AOwner: TComponent); override;  
  6.  end;  
  7. constructor TDemo.Create(AOwner: TComponent);  
  8. begin  
  9.  inherited Create(AOwner);  
  10. end;  
  11.   
  12. //如果调用的是与自身同名的方法, 则也可以省去方法名和参数.如上例中的  
  13. inherited Create(AOwner);  
  14. //可以改成:  
  15. Inherited;  

initialization:

[delphi]  view plain  copy
  1. //initialization关键字标识了单元被载入时所要调用的方法,   
  2. //通常是初始化一些不能自动初始化的对象, 也可以不用.  
  3. //initialization最常用的情况是对OLE对象做初始化.  
  4. initialization  
  5.  ActiveX.OleInitialize(nil);  
  6. finalization  
  7.  ActiveX.OleUninitialize;  

inline:

[delphi]  view plain  copy
  1. //InLine关键字用于Asm或assembler结构中,   
  2. //用于指出该汇编语句是向下兼容的.它对于程序的编译没有任何影响.  
  3. function IntToStr(Value: Integer): string;  
  4. asm  
  5.  InLine;  
  6.  PUSH ESI  
  7.  MOV  ESI, ESP  
  8.  SUB  ESP, 16  
  9.  xor  ECX, ECX  
  10.  PUSH EDX  
  11.  xor  EDX, EDX  
  12.  CALL CvtInt  
  13.  MOV  EDX, ESI  
  14.  POP  EAX  
  15.  CALL System.@LStrFromPCharLen  
  16.  ADD  ESP, 16  
  17.  POP  ESI  
  18. end;  

interface:

[delphi]  view plain  copy
  1. //Interface标识了单元中的接口部分, 单元的基本结构为:  
  2. //Unit...Interface...implementation...end.  
  3. //函数, 过程等的声明必须写在Interface关键字后.  
  4. //如果在Interface后引用对象, 则对象是没有实例的, 使用时必须被实例化.  
  5. Interface  
  6.  uses frmAbout;  
  7. var  
  8.  FAbout: TFormAbout;  
  9. begin  
  10.  FAbout := TFormAbout.Create(Self);  
  11.  FAbout.Show;  
  12. end;  
  13. //一个完整的单元必须拥有Interface部分.  
  14.   
  15. //Interface也可以用作接口的声明.  
  16. type  
  17.  IMalloc = interface(IInterface)  
  18.  ['{00000002-0000-0000-C000-000000000046}']  
  19.   function Alloc(Size: Integer): Pointer; stdcall;  
  20.   function Realloc(P: Pointer; Size: Integer): Pointer; stdcall;  
  21.   procedure Free(P: Pointer); stdcall;  
  22.   function GetSize(P: Pointer): Integer; stdcall;  
  23.   function DidAlloc(P: Pointer): Integer; stdcall;  
  24.   procedure HeapMinimize; stdcall;  
  25.  end;  

is:

[delphi]  view plain  copy
  1. //Is关键字用于对象的判断, 有某些情况下, 也可以作"As"使用.  
  2. var  
  3.  Comp: TComponent;  
  4. begin  
  5.  ...  
  6.  if Comp Is TEdit then  
  7.   (Comp as TEdit).Text := 'Edit';  
  8. end;  

label:

[delphi]  view plain  copy
  1. //label关键字用于声明行号标签, 以便用Goto进行转向, 不推荐使用.  
  2. var  
  3.  a,b: Integer;  
  4. label  
  5.  X,Y;  
  6. begin  
  7.  if a > b then  
  8.   goto X  
  9.  else  
  10.   goto Y;  
  11. X:  
  12.  WriteLn('a>b');  
  13. Y:  
  14.  WriteLn('b>a');  
  15. end;  

library:

[delphi]  view plain  copy
  1. //Library关键字用于指出一个工程为类库.类库编译后生成DLL文件, 可被其他程序调用.  
  2. library Editors;  
  3. uses EdInit, EdInOut, EdFormat, EdPrint;  
  4. exports  
  5.  InitEditors,  
  6.  doneEditors name done,  
  7.  InsertText name Insert,  
  8.  DeleteSelection name Delete,  
  9.  FormatSelection,  
  10.  PrintSelection name Print,  
  11.  SetErrorHandler;  
  12. begin  
  13.  InitLibrary;  
  14. end.  

message:

[delphi]  view plain  copy
  1. //Message关键字用于声明消息方法,   
  2. //带有Message的方法必须指出接收的消息类型, 并通过引用将消息传入方法中, 以便进行处理.  
  3. procedure Refresh(var Msg: TMessageRecordtype); messageID_REFRESH;  
  4. procedure Refresh(var Msg: TMessageRecordtype);  
  5. begin  
  6. if Chr(Msg.Code) = #13 then  
  7. ...  
  8. else  
  9. inherited;  
  10. end;  
  11.   
  12. //用户可以自定义消息, 自定义消息也能够被Message接收, 并引发事件.  

mod:

[delphi]  view plain  copy
  1. //Mod用于求两数之整数模, 即余数.用于Mod运算的两个数值必须均为整型, 其运算结果也为整型.  
  2. var  
  3.  a,b,c: Integer;  
  4. begin  
  5.  a := 20; b := 3;  
  6.  c := a mod b; {2}  
  7. end;  

name:

[delphi]  view plain  copy
  1. //Name关键字用于指出方法的别名,   
  2. //对于一个要被外部引用的方法, 建议用Name申请方法别名, 以避免外部程序改动方法的实体内容.  
  3. //从外部引用一个方法时, 如果该方法有别名, 则必须用Name进行标识.  
  4. function MessageBox(HWnd: Integer; Text, Caption: PChar; Flags: Integer): Integer;   
  5.  stdcall; external 'user32.dll' name 'MessageBoxA';  

near:

[delphi]  view plain  copy
  1. //Near标明了函数调用协定, 指出函数可以被本地调用.  
  2. //其他程序可以用dll的形式调用程序内的函数.它是向下兼容的.  
  3. function Add(a,b: Integer): Integer; near;  
  4. //如果这个程序被编译为Demo.exe, 并且另一个处于本地的程序需要调用这个函数, 可以使用以下语句:  
  5. function Add(a,b: Integer): Integer; stdcall; external 'Demo.exe';  

nil:

[delphi]  view plain  copy
  1. //Nil用于表示一个空指针, 或是没有实例的对象.  
  2. while Node <> nil do  
  3. begin  
  4.  ListBox1.Items.Add(Node.Text);  
  5.  Node := Node.GetNext;  
  6. end;  

nodefault:

[delphi]  view plain  copy
  1. //NoDefault关键字指出了一个属性不允许有默认值, 这通常用在继承中.  
  2. type  
  3.  TClassA = class  
  4.  private  
  5.   fValue: Integer;  
  6.  published  
  7.   property Value: Integer read fValue write fValue default 0;  
  8.  end;  
  9.  TClassB = class(TClassA)  
  10.  published  
  11.   property Value:Integer read fValue write fValue nodefault;  
  12.  end;  
  13.   
  14. //由上例可知, TClassA中的Value有默认值0,   
  15. //TClassB继承了TClassA, 所以也继承了其默认值, 在此用NoDefault去掉默认值  

not:

[delphi]  view plain  copy
  1. //Not用于取反, 它否定了原先的结果.例如:  
  2. if a > b then  
  3. //可以写成:  
  4. if not(a < b) then  
  5. //Not关键字通常用于切换Boolean型的属性  
  6. procedure Button1Click(Sender: TObject);  
  7. begin  
  8.  StatusBar1.Visible := not StatusBar1.Visible;  
  9. end;  

object:

[delphi]  view plain  copy
  1. //Object用于声明一个对象, 这个对象可以是任意的, 并且向下兼容.Object只能被Object所继承.  
  2. //声明对象的方法与声明类的方法是相同的.  
  3. type  
  4.  ODemoA = object  
  5.  end;  
  6.  ODemoB = object(ODemoA)  
  7.  end;  
  8.   
  9. //Object关键字还用于声明动态函数或过程, 例如:  
  10. type  
  11.  TMyFun = function(i: Integer): Integer of Object;  
  12.  TMyProc = procedure(s: stringof object;  
  13.   
  14. //经过object声明的函数或过程可以被动态的绑定到指定的函数体, 或是绑定到控件是事件中.  

of:

[delphi]  view plain  copy
  1. //Of关键用于和其他关键字构成指定的结构.Of可以与Case, Class, Array, File, Set, Object连用.  
  2. //Case语句:  
  3. case Tag Of  
  4.  0: Result := 'a';  
  5.  1: Result := 'b';  
  6. end;  
  7.   
  8. //Class语句:  
  9. type  
  10.  TDemo = class of TComponent;  
  11.   
  12. //Array结构:  
  13. var  
  14.  MyInt: array of Integer;  
  15.   
  16. //File结构:  
  17. var  
  18.  MyFile: file of Byte;  
  19.   
  20. //Set语句:  
  21. type  
  22.  TCol = (cA,cB,cC);  
  23.  TCols = set of TCol;  
  24.   
  25. //Object结构:  
  26. type  
  27.  MyFun = function(I: Integer): Integer of Object;  

on:

[delphi]  view plain  copy
  1. //On关键字用于异常处理, 指出发生的异常, 并获取异常信息.  
  2. try  
  3.  i := StrToInt(s);  
  4. except  
  5.  on E: exception do  
  6.   ShowMessage(E.Message);  
  7. end;  

or:

[delphi]  view plain  copy
  1. //一、表示逻辑或  
  2. if (a>0or (b>0then  
  3. //二、表示位运算  
  4. var  
  5. a,b,c: Integer;  
  6. begin  
  7. c := (a or b);  
  8. end;  
  9.   
  10. //使用Or表示逻辑时, Or左右的表达式必须用小括号括起, 以避免以生条件的冲突  
  11. //如果在条件语句中使用 Or, 则编辑器不知道用户使用Or做什么  
  12. 例如:  
  13. if a>0 or b>0 then  
  14. //编译器可能会理解为:  
  15. if a>(0 or b)>0 then  
  16. //或者  
  17. if (a>0or (b>0then  
  18. //但是实际编译时, 编译器会产生一个冲突, 报告错误  
  19. //并且第一种可能包含了a>b>c的形式, 这在Delphi中不被支持  
  20. //所以使用Or运算符时必须使用括号, 以区分左右的条件.  
  21. //表示位运算时也必须加上括号, 将Or以及左右参数括起.  

out:

[delphi]  view plain  copy
  1. //Out关键字说明了方法参数的输出方式, 一般的函数只能有一个返回值,   
  2. //使用Out可以在一个函数中返回多个结果.  
  3. //Out和var不同, Out是以返回值的形式进行参数返回, 而var是直接输入一个参数的地址.  
  4. procedure X(out i: Integer; out s: string);  
  5. begin  
  6.  i := i * 2;  
  7.  s := s + 'abc';  
  8. end;  
  9. procedure TForm1.Button1Click(Sender: TObject);  
  10. var  
  11.  i: Integer;  
  12.  s: string;  
  13. begin  
  14.  i := 20;  
  15.  s := 'xxx';  
  16.  X(i,s);  
  17. end;  

overload:

[delphi]  view plain  copy
  1. //Overload关键字指出了用于重载的方法, 重载即方法名相同,   
  2. //但是参数数量, 类型或顺序不同, 满足此条件的构成重载.  
  3. function X(i: Integer): string; overload;  
  4. function X(s: string): string; overload;  
  5. //从父类继承时, 如果子类拥有和父类相同的方法, 则也必须用overload构成重载,   
  6. //但是此类重载也必须满足重载的要求.  
  7. type  
  8.  TDemo = class(TComponent)  
  9.  public  
  10.   procedure CreateWnd(AOwner: TWinControl); overload;  
  11.  end;  
  12.   
  13. //如上例, 子类拥有的方法为:  
  14. procedure CreateWnd; {继承自父类}  
  15. procedure CreateWnd(AOwner: TWinControl); {子类声明}  
  16. //共两个CreateWnd方法.  
  17.   
  18. //如果不使用重载, 则在子类中可以覆盖父类的方法.  

override:

[delphi]  view plain  copy
  1. //Override用于覆盖一个Virtual或是Dynamic形式的方法.  
  2. //覆盖时必须沿用被覆盖方法的声明, 并且不允许修改原方法的参数和返回类型.  
  3. procedure Create(AOwner: TComponent); override;  
  4. //Override多用于继承, 用子类覆盖掉父类的方法.  
  5. type  
  6.  TClassA = class  
  7.   procedure X; virtual;  
  8.  end;  
  9.   
  10.  TClassB = class(TClassA)  
  11.   procedure X; override;  
  12.  end;  
  13.   
  14. //如上例, 子类拥有的方法为:  
  15. procedure X; {从父类覆盖}  
  16. //父类拥有的方法为:  
  17. procedure X; {父类自身方法, 未被覆盖}  
  18.   
  19. //如果父类的方法未用Virtual或Dynamic声明,   
  20. //或是有修改参数的需要, 则必须用Reintroduce关键字进行覆盖.  

package:

[delphi]  view plain  copy
  1. //Package关键字用于指出一个工程为控件库.  
  2. //控件库编译后生成BPL文件, 可被安装到Delphi的控件库中, 从而在以后的开发中使用控件.  
  3. package DATAX;  
  4.  requires  
  5.   rtl,  
  6.   clx;  
  7.  contains  
  8.   MyUnit in 'C:\MyProject\MyUnit.pas';  
  9. end.  

packed:

[delphi]  view plain  copy
  1. //Packed关键字用于对结构体记录或数组进行打包, 打包后被打包对象的体积能显著减小.  
  2. type  
  3.  TPerson = packed Record  
  4.   PName: string[32];  
  5.   PAge: Integer;  
  6.  end;  
  7.  MyArray: packed array of PChar;  

pascal:

[delphi]  view plain  copy
  1. //Pascal标明了函数调用协定,   
  2. //指出函数在调用时遵循Pascal原因, 即先对所有的变量进行初始化,   
  3. //避免因异步线程调用而产生的错误.它是向下兼容的.  
  4. function X(i: Integer): Integer; Pascal;  
  5. begin  
  6.  Result := i * 2;  
  7. end;  

private:

[delphi]  view plain  copy
  1. //Private标明了类内元素的访问区分权限, 被Private区分的元素只能被本类内部访问.  

procedure:

[delphi]  view plain  copy
  1. //Procedure用于声明过程  
  2. procedureX(i: Integer);  
  3. //它也可以用于动态函数的声明  
  4. type  
  5.  TProc = procedure(i: Integer) of object;  
  6.   
  7. //动态声明时, 不需要指出过程名, 只需要指出参数就可以, 具体的过程名可以在后期绑定.  

program:

[delphi]  view plain  copy
  1. //Program关键字用于指出一个工程为应用程序.控件库编译后生成exe文件, 可以直接执行  
  2. program Project1;  
  3. uses  
  4.  Forms,  
  5.  Unit1 in 'Unit1.pas' ;  
  6. {$R *.res}  
  7. begin  
  8.  Application.Initialize;  
  9.  Application.CreateForm(TForm1, Form1);  
  10.  Application.Run;  
  11. end.  

property:

[delphi]  view plain  copy
  1. //Property关键字用于声明属性, 属性分为显式属性和隐式属性两种,   
  2. //只有声明在published访问区分符下的属性才是显式属性, 可以直接在对象查看器中查看.  
  3. type  
  4.  TDemo = class  
  5.  Private  
  6.   fValue: Integr;  
  7.  Published  
  8.   property Value: Integer read fValue write fValue;  
  9.  end;  
  10. //事件也是属性的一种, 可以在published区分符下用Property进行声明  
  11. type  
  12.  TOnTextChange=procedure (Sender: TObject) of object;  
  13.  TDemo = class  
  14.  private  
  15.   fEvent: TOnTexChange;  
  16.  published  
  17.   property OntextChange: TOnTextChange read fEvent write fEvent;  
  18.  end;  

protected:

[delphi]  view plain  copy
  1. //Protected标明了类内元素的访问区分权限, 被Protected区分的元素只能被本类内部和其子类访问.  

public:

[delphi]  view plain  copy
  1. //Public标明了类内元素的访问区分权限, 被Public区分的元素能够被类内和类外任何对象访问.  

published:

[delphi]  view plain  copy
  1. //Published标明了类内元素的访问区分权限.  
  2. //被Published区分的元素能够被类内和类外任何RTTI对象访问  
  3. //只在声明在Published区分符下的属性才能够成为显式属性并在对象查看器中显示.  

raise:

[delphi]  view plain  copy
  1. //Raise语句用于抛出异常,   
  2. //如果希望通过外部程序处理异常, 或是在异常发生时重新将异常抛出, 可以使用Raise语句.  
  3. function GetString(i: Integer): string;  
  4. begin  
  5.  if i < 0 then  
  6.   raise exception.Create('Integer Cannot smaller than 0');  
  7.  Result := IntToStr(i);  
  8. end;  
  9. //在异常处理中, 可以重新抛出异常  
  10. try  
  11.  i := StrToInt(s);  
  12. except  
  13.  on E: exception do  
  14.   raise exception.Create(E.Message);  
  15. end;  

read:

[delphi]  view plain  copy
  1. //Read用于标识属性中读取所使用的成员或方法.  
  2. private  
  3.  fValue: Integer;  
  4. published  
  5.  property Value: Integer readfValue;  
  6. //上例中即表明Value属性的值从fValue成员上读取.  

readonly:

[delphi]  view plain  copy
  1. //ReadOnly关键字用于标识一个对象是否只读.  
  2. propertyReadOnly;  
  3. //当ReadOnly设为True时, 不允许用户手动修改属性, 只能通过其他对象来操作.  

record:

[delphi]  view plain  copy
  1. //Record关键字用于声明一个结构体记录,   
  2. //一个结构体可以视为一个不需要实例化的对象, 拥有自己的成员.  
  3. type  
  4.  TPerson = record  
  5.   PName: string[32];  
  6.   PAge: Integer;  
  7.  end;  

register:

[delphi]  view plain  copy
  1. //Register标明了函数调用协定, 指出函数在被调用时可以在注册表内留下记录.它是向下兼容的.  
  2. functionAdd(a,b: Integer): Integer; Register; Register  
  3. //关键字还用于向控件库或是IDE注册控件或是专家工具.  
  4. procedure Register;  
  5. begin  
  6.  RegisterComponents('Sample', [TDemo]);  
  7. end;  

reintroduce:

[delphi]  view plain  copy
  1. //Reintroduce用于重新发布方法, 通常用于继承时,   
  2. //如果要覆盖的方法是静态方法, 或是需要修改方法的参数等, 必须用Reintroduce进行重发布.  
  3. //对于Virtual或Dynamic方法, 可以直接用Override进行覆盖.  
  4. type  
  5.  TClassA = class  
  6.   procedure X;  
  7.  end;  
  8.  TClassB = class(TClassA)  
  9.   procedure X; reintroduce;  
  10.  end;  
  11.  TClassC = class(TClassB)  
  12.   procedure X(i: Integer); reintroduce;  
  13.  end;  

repeat:

[delphi]  view plain  copy
  1. //repeat关键字用于引出repeat循环结构,   
  2. //该循环必须先执行一次循环体, 然后再对循环条件进行判断.repeat必须与Until关键字联合使用.  
  3. i := 0;  
  4. repeat  
  5.  sum := sum + i;  
  6.  Inc(i);  
  7. until(i >= 100);  

requires:

[delphi]  view plain  copy
  1. //Requires关键字指出了编译Package时的必备条件.若Requires的条件未满足, 则不允许编译包.  
  2. package DATAX;  
  3.  requires  
  4.   rtl,  
  5.   clx;  
  6. end.  

resourcestring:

[delphi]  view plain  copy
  1. //ResourceString用于声明资源字符串, 资源字符串可以在被声明的结构内使用.  
  2. ResourceString  
  3.  CreateError = 'Cannot create file %s';  
  4.  OpenError = 'Cannot open file %s';  
  5.  LineTooLong = 'Line too long';  
  6.  ProductName = 'Borland Rocks';  
  7.  SomeResourceString = SomeTrueConstant;  

safecall:

[delphi]  view plain  copy
  1. //Safecall是函数调用协定的一种, 它规定了被COM调用的函数所必须遵守和规则.  
  2. //在编译时, Safecall声明的函数被编译成COM接口兼容的.  
  3. procedure X(s: WideString); safecall;  
  4. //在编译后成为:  
  5. procedure X(s: PAnsiString);  

set:

[delphi]  view plain  copy
  1. //Set关键字用于声明集合类, 集合类允许用集合运算符, 如in等进行操作.  
  2. type  
  3.  TCol = (cA,cB,cC);  
  4.  TCols = set ofTCol;  
  5. //操作时允许使用加减符号来添加或删除某个集合元素  
  6. var  
  7.  Cols: Tcols;  
  8. begin  
  9.  Cols := Cols + [cA,cB];  
  10. end;  

shl:

[delphi]  view plain  copy
  1. //SHL表示向左移位, 左移的位数即乘以2的幂数  
  2. var  
  3.  x: Integer;  
  4. begin  
  5.  X := 2 shl 3{16}  
  6. end;  

shr:

[delphi]  view plain  copy
  1. //SHR表示向右移位, 右移的位数即除以2的幂数  
  2. var  
  3.  x: Integer;  
  4. begin  
  5.  X := 16 shr 2{4}  
  6. end;  

stdcall:

[delphi]  view plain  copy
  1. //Stdcall是函数调用协定的一种, 它规定了能让程序调用的函数所应遵守的规则.  
  2. //Stdcall关键字必须在主调方和被调方之间形成配对.  
  3. //例如, 被调方函数:  
  4. Library Demo;  
  5. function X(i: Integer): Integer; stdcall;  
  6. begin  
  7.  Result := i * 2;  
  8. end;  
  9. exports  
  10.  X;  
  11. begin  
  12. end.  
  13.   
  14. //主调方函数:  
  15. function X(i: Integer): Integer; stdcall; external 'Demo.dll';  
  16.   
  17. //同时需要注意, 使用Stdcall关键字时, 被调函数是大小写敏感的, 此处极容易出错.  

stored:

[delphi]  view plain  copy
  1. //Stored用于指出一个属性的值是否能被保留, 若指定了True, 则允许对属性值进行赋值撤销的操作.  
  2. property Value: string read fValue write fValue stored True;  

string:

[delphi]  view plain  copy
  1. //String是一个数据类型, 它代表了字符串.  
  2. var  
  3.  Str: string;  

then:

[delphi]  view plain  copy
  1. //Then关键字用于If语句中, 当If条件成立时, 执行Then后的语句.  
  2. var  
  3.  a,b: Integer;  
  4. begin  
  5.  if a > b then  
  6.   WriteLn('a')  
  7.  else  
  8.   WriteLn('b');  
  9. end;  

threadvar:

[delphi]  view plain  copy
  1. //Threadvar标识了一个随线程启动而创建的变量,   
  2. //如果用Threadvar声明变量, 则在程序结束前必须手动释放其占用的空间.  
  3. threadvar S: AnsiString;  
  4. S := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';  
  5. S := '';  
  6. //S := ''; 即释放变量S所占用的内存.  

to:

[delphi]  view plain  copy
  1. //To关键字用于For语句, 指明循环变量是递增的.  
  2. for i := 10 to 100 do  
  3.  ListBox1.Items.Add(IntToStr(i));  
  4. //在For语句中, 循环变量递增用To关键字, 递减用DownTo关键字.  

try:

[delphi]  view plain  copy
  1. //try语句用于异常处理, 对于有可能发生异常的语句, 可以放在try结构下, 以便对其进行异常保护.  
  2. try  
  3.  i := StrToInt(s);  
  4. except  
  5.  ShowMessage('Error');  
  6. end;  

type:

[delphi]  view plain  copy
  1. //Type关键字用于声明各种对象, 用Type关键字声明的对象, 在传递时按引用传递.  
  2. type  
  3.  TDemo = class  
  4.  end;  
  5. //type也用来声明枚举类型或是按引用传递的变量.  
  6. type  
  7.  TCol = (cA,cB,cC);  
  8.  TInt = Integer;  

unit:

[delphi]  view plain  copy
  1. //Unit标识了单元的开头, 单元的基本结构为 Unit...Interface...implementation...end.  
  2. Unit Unit1;  
  3. Interface  
  4.  uses Classes;  
  5. implementation  
  6. end.  
  7. //一个完整的单元必须拥有Unit作为开头.  

until:

[delphi]  view plain  copy
  1. //Until关键字用于判断repeat循环结构的循环条件,   
  2. //如果循环条件为真, 则退出循环.Until必须与repeat关键字联合使用.  
  3. i := 0;  
  4. repeat  
  5.  sum := sum + i;  
  6.  Inc(i);  
  7. until(i >= 100);  

uses:

[delphi]  view plain  copy
  1. //Uses用于引用一个外部的单元, 并且能够使用该单元中的公共部分.  
  2. //Uses语句通常放在一个单元的接口或是实现部分.  
  3. Interface  
  4.  uses Classes;  
  5. Implemention  
  6.  uses frmAbout;  

var:

[delphi]  view plain  copy
  1. //var关键字用于声明一个变量或是对象, 用var声明的变量接值传递.  
  2. var  
  3.  i: Integer;  
  4.  s: string;  
  5. //var也可以用于标识按引用传递的方法参数  
  6. function X(var i: Integer): Integer;  
  7.   
  8. //上述函数中的参数i即按引用传递, 它的值可以在函数执行时被改变, 并返回主调函数.  

varargs:

[delphi]  view plain  copy
  1. //varArgs标识了引用参数, 它必须和Cdecl关键字联用, 表明允许调用的函数使用引用传递.  
  2. function printf(Format: PChar): Integer; cdecl; varargs;  
  3. //上述代码从C++的类库中引用了Printf函数, 并允许按引用的方式传入参数.  

virtual:

[delphi]  view plain  copy
  1. //Virtual用于声明一个虚方法,   
  2. //虚方法可以被覆盖, 并且可以使程序运行速度尽可能的快(区别于Dynamic).  
  3. procedure X(i: Integer); virtual;  

while:

[delphi]  view plain  copy
  1. //While关键字用于引出While循环语句, 循环前先进行循环条件的判断, 如果条件为真则执行循环.  
  2. i := 0;  
  3. while i < 100 do  
  4. begin  
  5.  sum := sum + i;  
  6.  Inc(i);  
  7. end;  

with:

[delphi]  view plain  copy
  1. //With关键字用于将相同的对象集合起来处理, 它可以省去输入大量重复的代码, 使代码看上去比较精简.  
  2. with Form1.Memo1.Lines do  
  3. begin  
  4.  Clear;  
  5.  Append('abc');  
  6.  Append('def');  
  7.  SaveToFile('C:\demo.txt');  
  8. end;  
  9. //上面这段代码如果不使用With语句, 则显得非常冗余复制内容到剪贴板代码:  
  10. Form1.Memo1.Lines.Clear;  
  11. Form1.Memo1.Lines.Append('abc');  
  12. Form1.Memo1.Lines.Append('def');  
  13. Form1.Memo1.Lines.SaveToFile('C:\demo.txt');  

write:

[delphi]  view plain  copy
  1. //Write用于标识属性中写入所使用的成员或方法.  
  2. private  
  3.  fValue: Integer;  
  4. published  
  5.  property Value: Integer writefValue;  
  6. //上例中即表明Value属性的值写入到fValue成员上.  

writeonly:

[delphi]  view plain  copy
  1. //writeonly关键字用于标识一个对象是否只写.  
  2. property writeonly;  
  3. //当writeonly设为True时, 不允许用户读取属性, 只能通过其他对象来操作.  

xor:

[delphi]  view plain  copy
  1. //Xor用于取异或, 当两个操作数相等时, 返回False, 不等时返回True.  
  2. var  
  3.  a,b: Integer;  
  4. begin  
  5.  a := 2; b := 3;  
  6.  if a xor b then  
  7.   WriteLn('a xor b')  
  8.  else  
  9.   WriteLn('a not xor b');  
  10. end;  
  11. //Xor也用于计算异或值  
  12. WriteLn(IntToStr(3 xor 5)); {6}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值