本文詳細介紹了Delphi中常用的各個關鍵字名稱及用法,供大家在程式設計過程中藉鏡參考之用。詳情如下:
absolute:
//它使得你能夠建立一個新變數, 並且該變數的起始位址與另一個變數相同.var Str: string[32]; StrLen: Byte absoluteStr;//這個宣告指定了變數StrLen起始位址與Str相同.//由於字串的第0個位置保存了字串的長度, 所以StrLen的值即字串長度.beginStr := 'abc';Edit1.Text := IntToStr(StrLen);end;
abstract:
//它允許你創建抽象的方法, 包括有抽象方法的類別稱為抽象類別.//Abstract關鍵字必須與Virtual或Dynamic關鍵字同時使用, 因為抽象方法必須被覆蓋式實現.//抽象類別不能實例化, 抽象方法不能包含方法體.type TDemo = class private protected procedure X; virtual; abstract; public constructor Create; destructor Destroy; override; published end;
and:
//一、表示邏輯與if (a>0) and (b>0) then//二、表示位運算vara,b,c: Integer;beginc := (a and b);end;//使用And表示邏輯時, And左右的表達式必須用小括號括起, 以避免以生條件的衝突.//例如:if a>0 and b>0 then//編譯器可能會理解為:if a>( 0 and b)>0 then//或:if (a>0) and (b>0) then//但是實際編譯時, 編譯器會產生一個衝突, 報告錯誤.//並且第一種可能包含了a>b>c的形式, 這在Delphi中不被支援.//所以使用And運算子時必須使用括號, 以區分左右的條件.//表示位運算時也必須加上括號, 將And以及左右參數括起.
array:
//Array用來表示數組, 任何的物件都能被宣告成數組.數組分為靜態和動態的2種.//靜態數組varArr1: array [1..10] of Integer;//動態數組, 由於宣告時不知其元素個數, 所以必須在後期用SetLength方法設定數組的大小varArr2: array of Integer;//數組作為參數時, 不能傳入數組的大小,只能傳入陣列名, 然後用Length方法取得陣列的元素個數function X(A: array of Integer): Integer;vari: Integer;beginResult := 0;for i := 0 to Length(A)-1 doResult := Result + A[i];end;
as:
//As用於將一個物件轉換為另一個物件procedure BtnClick(Sender:TObject);begin (Sender as TButton).Caption := 'Clicked';end;//對於物件填充介面的轉換, 必須用As進行(HTTPRIO as IExp).GetConnection;//As不能用於資料類型的轉換, 下面的程式碼是錯誤的:vari: Integer;s: string;begins := (i as string);end;//正確寫法是:s := string(i);
asm:
//Asm關鍵字用於插入彙編程式碼, 使用彙編程式碼時, 必須使用asm...end;的結構, 而非begin...end;function IntToHex(Value: Integer; Digits: Integer): string;asm CMP EDX, 32 JBE @A1 xor EDX, EDX @A1: PUSH ESI MOV ESI, ESP SUB ESP, 32 PUSH ECX MOV ECX, 16 CALL CvtInt MOV EDX, ESI POP EAX CALL System.@LStrFromPCharLen ADD ESP, 32 POP ESIend;
assembler:
//Assembler關鍵字用於支援早期的彙編, 如80386等.//它和Asm的區別:Asm允許使用Win32彙編, 而Assembler只允許80x86彙編, 它不允許Invoke語句的出現.function IntToHex(AValue: Int64): string; assembler;
automated:
//Automated存取區分符用於描述一個自動類型的成員, 它能夠使程式的版本向下相容.//ComObj單元內的成員及其實例不能使用Automated存取區分符.type TDemo = class automated Str:WideString ; end;//在程式的下一個版本, 將Str做了修改, 變成typeTDemo = classautomatedStr: AnsiString;end//則新版本的Str變數能夠接受舊版的WideString型資料, 並自動轉換成AnsiString.//在實際開發中, 如果沒有特殊的需要, 一般不用automated存取區分符.
begin:
//begin關鍵字用來表示一段程式或一個結構的開始, 必須用end關鍵字來結束.procedure X;begin ShowMessage('A Demo');end;//一般的結構, 如If, For, While等等也需要用begin關鍵字來標示結構起始點for i:=1 to 100 dobeginsum := sum + i;if sum > 1000 then Break;end;
case:
//Case語句用於完成條件選擇, Case語句的被選擇物件必須是有序型別, 包含整數, 枚舉型別, 字元型等.//Case語句必須由end結束,如果沒有相符合的選擇項目, 可以加入else來作出通用選擇.function GetDays(AYear,AMonth: Integer): Integer;begin case AMonth of 1,3,5,7,8,10,12: Result := 31; 4,6,9,11: Result := 30; 2: begin if IsLeapYear(AYear) then Result:=29 else Result:=28; end; else Result:=0;end;
cdecl:
//Cdecl是函數呼叫協定的一種, 它規定了從C或C++編寫的DLL中呼叫函數所必須遵守的規則.//它可以將C或C++中的資料型別轉換為Delphi的.//例如C++中的程式碼:int X(int i){ return i*2;}//這個函式被編譯在Demo.dll中, 用Delphi呼叫時必須使用:function X(i: Integer): Integer; Cdecl; external 'Demo.dll';
class:
//Class關鍵字用於宣告或繼承一個類別, 也可以讓類別和介面同時繼承.//另外, Class關鍵字也能用於宣告類別通用方法, 使得父類別可以從類別內存取子類別的方法.type ClassDemo = class(TObject) private public constructor Create; end;//如果用class宣告方法, 則該方法在類別與相關類別中都可以使用, 譬如:typeClassA = classprivatepublicprocedure Y;end;typeClassB = class(ClassA)privatepublicclass procedure X;end;//則在使用時ClassA能夠直接存取ClassB的X方法procedure ClassA.Y;beginSelf.X;end;//此時父類將子類別的class方法作為自身的方法進行呼叫.
const:
//Const關鍵字用於聲明常數, 使用const聲明的資料將不能在程式中被改變.//也可以用來聲明函數參數, 用const指定的參數不允許在函數中改變.const MyFileName = 'Delphi ';const MyInteger = 100;//用Const宣告常數不需要指出其資料型別, 系統會自動判斷型別, 並作自動調整.//函式中可以用const宣告不可變更的參數function X(const i: Integer): string;//此時在函數運算過程中, i的值不可改變.
constructor:
//constructor關鍵字用來宣告一個類別的建構子, 當類別被實例化時, 首先呼叫此函式//建構函式一般用Create表示, Create方法能夠連帶類別中存在的CreateWnd方法.type ClassDemo = class( TObject) private fValue: Integer; public constructor Create; end;constructor ClassDemo.Create;beginfValue := 0;end;
contains:
//Contains關鍵字指出了某個包(Package)是否包含某個文件.//用Contains引入的文件必須被添加到包文件中, 它可以避免關鍵文件的引用丟失.package DATAX; requires rtl, clx ; contains Db, DBLocal, DBXpress;end.
default:
//Default關鍵字用來指出一個屬性的預設值//只有有序型別的屬性才允許預設值的存在, 否則必須在建構函式中初始化屬性值.type ClassDemo = class private fValue: Integer; published property Value : Integer read fValue write fValue default 0; end;//它也可以指出一個類別的預設屬性property strings[Index: Integer]: string read GetString write PutString; Default;
destructor:
//Destructor用於標識析構函數, 析構函數在類別被釋放時自動呼叫.//析構函數只允許覆蓋, 再不允許重載.析構函數通常用Destroy作為函數名.type ClassDemo = class( TComponent) public destructor Destroy;override; end;//由於TComponent類別中也有Destroy方法, 所以要將其重寫//但是若要重載析構函數, 則不允許,下面程式碼是錯誤的:destructor Destroy; overload;
dispid:
//DispId關鍵字被用在DispInterface介面中, 用於指定特定的適配序號.//在DispInterface介面中, 適配序號必須是唯一的, //如果不指定DispId, 則系統會自動指派適配序號給介面內每一個方法.//可以透過適配序號存取DispInterface介面中的方法.type IStringsDisp = dispinterface ['{EE05DFE2-5549-11D0-9EA9-0020AF3D82DA}'] property ControlDefault[Index: Integer]: Olevariant dispid 0; default; function Count: Integer; dispid 1; property Item; default; function Count: Integer; dispid 1; property Item; Remove(Index: Integer); dispid 3; procedure Clear; dispid 4; function Add(Item: Olevariant): Integer; dispid 5; function _NewEnum: IUnknown; dispid -4; end;
dispinterface:
//DispInterface用來宣告一個特定的適配器介面, 這個適配器能夠接受標準系統介面中傳入傳出的資料.//用DispInterface宣告的介面不能被繼承, 只能夠被引用.//DispInterface中方法只能呼叫, 且必須被動態綁定.//可以透過DispId為介面內方漢分配適配序號.//DispInterface僅能用於Windows平台, 如果在Linux下進行開發,則此關鍵字會自動被系統屏蔽.//通常情況下, 不使用DispInterface.//實例請參閱DispId
div:
//Div用於求兩數之整數商。用於Div運算的兩個數值必須均為整數, 其運算結果也為整數.var a,b,c:Integer;begin a := 20; b := 3; c := a div b; {6}end;
do:
//Do關鍵字用於For, While, On, With語句, 構成特定的結構//For語句:for i := 1 to 100 do sum:=sum+i;//While語句:while i < 100 dobegin sum := sum + i; Inc(i);end;//On語句(異常處理):try i := StrToInt(s);except on exception do ShowMessage('Error!');end;//With語句:with Memo1.Lines dobegin Clear; Append('abc'); Append('123');end;
downto:
//DownTo關鍵字用於For語句, 指明循環變數是遞減的.for i := 100 downto 1 doListBox1.Items.Add(IntToStr(i));//在For語句中, 循環變數遞增用To關鍵字, 遞減用DownTo關鍵字.
dynamic:
//Dynamic用來宣告一個動態的方法, //動態方法可以被覆寫, 並且可以使程式碼大小盡可能的減少(區別於Virtual).procedure X(i: Integer); dynamic;
else:
//else用於引導程式的運行方向, 它可以與If, Case和On語句聯用, 當條件不滿足時, 轉到else下運行//If語句(在If語句中, else前不允許有分號):if a > b thenc := aelsec:=b;//Case語句:case Tag Of1:Result:=1;2:Result:=2;3:Result:=3;elseResult:=0;end;//On語句(異常處理):tryi := StrToInt(s);Excpeton EZeroDivide do Result : = 1;on EOverflow do Result := 2;elseResult := 0;end;
end:
//End用來結束一個語句塊或是一個單元.//它可以與begin, Case, Class, Interface, Asm, Unit, Package等相匹配.//對於語句塊(局部結束), End後必須添加分號.//而對於單元或包(全局結束), end後必須添加句號.//在If語句中else關鍵字前的End後不允許添加符號.procedure X;begin with Button1 do begin if Button1.ShowHint then Button1.Caption := 'Hinted' else Button1.Caption := 'Not Hinted'; end;end;//在套件內使用End來結束:package DATAX;requiresrtl,clx;contains Db, DBLocal, DBXpress ;end.
except:
//except關鍵字用於異常處理, 必須用在try語句內, 如果發生異常, 則執行except後的語句try i := StrToInt(s);except ShowMessage('Error!');end;
export:
//Export標示了函數呼叫協定, 指出函數可以被輸出, 輸出的函數能被本地或遠端呼叫.//其他程式可以用dll的形式呼叫程式內的函數.它是向下相容的.function Add( a,b: Integer): Integer; export;//如果這個程式被編譯為Demo.exe, 並且另一個程式需要呼叫這個函數, 可以使用以下語句function Add(a,b: Integer): Integer; stdcall; external 'Demo.exe';
exports:
//exports用於輸出物件, 它必須被用在介面和實作之間, 可以同時輸出多個項目, 項與項目之間用逗號分開.libraryDemo;function X(i: Integer): string; stdcall;begin Result:=IntToStr(i);end;exports X;beginend.//如果輸出的物件被重載, 則必須給物件起個別名, 並註明參數.library Demo;function X(i: Integer): string; overload; stdcall;begin Result := IntToStr(i);end;function X(s: string): Integer; overload; stdcall;begin Result := StrToInt(s); end;exportsX(i: Integer) name 'x1',X(s: string) name 'x2';beginend.
external:
//External關鍵字用來引用一個外部的或是OBJ內的方法.{$L Demo.OBJ}procedure X(i:Integer);external;//如果是從dll或外部程式中引用, 則可以使用以下程式碼:function A(FileName: string): string; external 'Demo.dll';//如果被引用的函數被重載, 則必須另外指出引用的名稱.function A(Name: string): string; overload; stdcall; external 'Demo.dll' name 'A1';function A(Code: Integer): string; overload; stdcall; external 'Demo.dll' name 'A2';// /使用External關鍵字時, 必須注意大小寫, 否則將出現錯誤.
far:
//Far標示了函數呼叫協定, 指出函數可以被遠端呼叫.//其他程式可以用dll的形式呼叫程式內的函數.它是向下相容的.functionAdd(a,b: Integer): Integer; Far ;//如果這個程式被編譯為Demo.exe, 且另一個處於其他電腦的程式需要呼叫這個函式, 可以使用以下語句:function Add(a,b: Integer): Integer; stdcall; external 'Demo.exe';
file:
//File關鍵字指出了檔案操作類型, 檔案必須被宣告為File, //如果在File後追加Of和檔案類型, 則檔案可以被定義為讀寫指定類型資料.type TPerson = record PName: string[ 32]; PAge: Integer; end;var PFile: file of TPerson;
finalization:
//finalization關鍵字標識了單元被釋放時所要調用的方法, //通常是釋放掉單元中不能自動釋放的對象, 也可以不用.//finalization最常用的情況是對OLE對像做反初始化.initialization ActiveX.OleInitialize(nil);finalization ActiveX.OleUninitialize;
finally:
//finally關鍵字指出了異常處理中最後必須要呼叫的方法, //不論是否發生異常, finally後的語句總是在try語句結束時執行.try Node := Node.GetNext; Edit1.Text := Node.Text;finally Node := nil;end;
for:
//For關鍵字引出For循環結構, 用來做指定次數的迴圈.for i := 1 to 100 dosum := sum + i;//如果循環變數是遞減的, 則可以用DownTo關鍵字for i : = 100 downto 1 do Inc(sum);
forward:
//Forward關鍵字用於方法的前置定義.只定義方法聲明, 然後在程式的後面對方法進行實現.//這麼做有利於程式碼的可讀性, 可以將所有的聲明放在一起,然後將所有的實作也放在一起.function X(i: Integer): Integer; forward;procedure Y(s: string); forward;...function X;begin Result := i * 2;end;procedure Y;beginWriteLn(s);end;//用Forward前置聲明的方法在實現時不需要再輸入方法的參數和返回值, 直接使用方法名即可.
function:
//Function用於宣告函數functionX(i: Integer): Integer;//它也可以用於動態函數的宣告type TFun = function(i: Integer): Integer of object;//動態宣告時, 不需要指出函數名稱, 只需要指出參數和返回類型就可以, 具體的函數名可以在後期綁定.
goto:
//Goto語句用在跳轉行號, 可以跳到當前結構層內任意位置.//必須在聲明處用label關鍵字聲明行號.//由於Goto語句會破壞程式的結構, 不建議使用.var a,b: Integer;label X,Y;begin if a > b then goto X else goto Y;X: WriteLn('a > b');Y: WriteLn('b > a');end;
if:
//If關鍵字引出If條件語句, 用於對條件進行判斷.var a,b: Integer;begin a := 2; b := 3; if a>b then WriteLn('a=' + IntToStr(a )) else WriteLn('b=' + IntToStr(b));end;//If語句的通常結構是If...Then...else, else語句也可以不要.//在If語句內如果有多個子語句, 則必須用begin...End結構進行區分.if a > b thenbegin WriteLn('a>b'); WriteLn('a=' + IntToStr(a)); WriteLn('b=' + IntToStr(b));Endelse WriteLn('b>a');
implementation:
//Implementation標識了單元中的實作部分, 單元的基本結構為://Unit...Interface...implementation...end.//函數體, 過程體等必須寫在implementation關鍵字後./ /如果在implementation後引用物件, 則物件是非公開的, 只能供單元自身使用.implementation uses frmAbout;begin FormAbout.Show;end;//一個完整的單元必須擁有implementation部分.
implements:
//Implements指出了一個屬性從介面繼承, 此時屬性被轉換成介面物件.//透過介面動態綁定屬性, 並動態的設定屬性值.type IMyInterface = interface procedure P1; procedure P2; end; TMyImplclass = class procedure P1; procedure P2; end; TMyclass = class(TInterfacedObject, IMyInterface) FMyImplClass: TMyImplClass; property MyImplClass: TMyImplclass read FMyImplclass implements IMyInterface; procedure IMyInterface.P1 = MyP1; procedure MyP1; end;//通過implements P1 = MyP1;
in:
//In用來判斷一個集合中是否包含某個元素.被判斷的內容必須是單一集合元素和一個集合的實例.type TCol = (cA,cB,cC); TCols = set of TCol;var Cols: TCols;begin Cols := [cA,cB]; if cA in Cols then ShowMessage('cA in Cols') else ShowMessage('cA not in Cols');end;//In也用於工程文件中, 用於標識某個文件是否被工程所引用.Uses Unit1 in 'Unit1.pas';//In可以被用在For語句中, 用於迴圈取出一個集合中的元素.var s: string; sl: TStringList;begin ... for s In sl do begin ShowMessage(s); end;end;
index:
//Index用於在屬性中標識序號, 以便用相同的屬性方法(Get,Set)對不同的屬性進行操作.type TForm1 = class(TForm) private function GetInfo(const Index: Integer): Longint; procedure SetInfo (const Index: Integer; const Value: Longint); public property iLeft:Longint index 0 read GetInfo write SetInfo; property iTop:Longint index 1 read GetInfo write SetInfo; property iWidth:Longint index 2 read GetInfo write SetInfo; property iHeight:Longint index 3 read GetInfo write SetInfo; end;function TForm1.GetInfo( begin case Index of 0: result := self.Left; 1: Result := self.Top; 2: result := self.Width; 3: result := self.Height; end;end;//Index關鍵字也用來在屬性中指出多個元素, 例如:property Selected[Index: Integer]: Boolean read GetSelected write SetSelected;
inherited:
//Inherited用來呼叫父類別的方法.type TDemo = class(TComponent) public constructor Create(AOwner: TComponent); override; end;constructor TDemo.Create(AOwner: TComponent);begin inheritedate(AOwner); Creend; //如果呼叫的是與自身同名的方法,則也可以省去方法名稱和參數.如上例中的inherited Create(AOwner);//可以改成:Inherited;
initialization:
//initialization關鍵字標識了單元被載入時所要呼叫的方法, //通常是初始化一些不能自動初始化的物件, 也可以不用.//initialization最常用的情況是對OLE物件做初始化.initialization ActiveX. OleInitialize(nil);finalization ActiveX.OleUninitialize;
inline:
//InLine關鍵字用於Asm或assembler結構中, //用於指出該彙編語句是向下相容的.它對於程式的編譯沒有任何影響.function IntToStr(Value: Integer): string;asm InLine; PUSH ESI MOV ESI, ESP SUB ESP, 16 xor ECX, ECX PUSH EDX xor EDX, EDX CALL CvtInt MOV EDX, ESI POP EAX CALL System.@LStrFromPCharLen ADD ESP, 16 POP ESIend;
interface:
//Interface標識了單元中的介面部分, 單元的基本結構為://Unit...Interface...implementation...end.//函數, 程序等的聲明必須寫在Interface關鍵字後./ /如果在Interface後面引用物件, 則物件是沒有實例的, 使用時必須被實例化.Interface uses frmAbout;var FAbout: TFormAbout;begin FAbout := TFormAbout.Create(Self); FAbout.Show;end;//一個完整的單元必須擁有Interface部分.//Interface也可以用作接口的聲明.type IMalloc = interface(IInterface) ['{00000002-0000-0000-C000-000000000046}'] function Alloc(Size: Integer): Pointer; stdcall; function Realloc(P: Pointer; Size: Integer): Pointer; stdcall; procedure Free(P: Pointer); stdcall; function GetSize(P: Pointer): Integer; stdcall; function DidAlloc(P: Pointer): Integer; stdcall; procedure HeapMinimize stdcall; end;
is:
//Is關鍵字用於物件的判斷, 有某些情況下, 也可以作"As"使用.var Comp: TComponent;begin ... if Comp Is TEdit then (Comp as TEdit).Text := 'Edit ';end;
label:
//label關鍵字用於宣告行號標籤, 以便用Goto進行轉向, 不推薦使用.var a,b: Integer;label X,Y;begin if a > b then goto X else goto Y;X: WriteLn( 'a>b');Y: WriteLn('b>a');end;
library:
//Library關鍵字用來指出一個工程為類別庫.類別庫編譯後產生DLL檔, 可被其他程式呼叫.library Editors;uses EdInit, EdInOut, EdFormat, EdPrint;exports InitEditors, doneEditors name done, InsertText name Insert , DeleteSelection name Delete, FormatSelection, PrintSelection name Print, SetErrorHandler;begin InitLibrary;end.
message:
//Message關鍵字用於宣告訊息方法, //帶有Message的方法必須指出接收的訊息類型, 並透過引用將訊息傳入方法中, 以便進行處理.procedure Refresh(var Msg: TMessageRecordtype); messageID_REFRESH; procedure Refresh(var Msg: TMessageRecordtype);beginif Chr(Msg.Code) = #13 then...elseinherited;end;//使用者可以自訂訊息, 自訂訊息也能夠被Message接收, 並引發事件.
mod:
//Mod用於求兩數之整數模, 即餘數.用於Mod運算的兩個數值必須均為整數, 其運算結果也為整數.var a,b,c: Integer;begin a := 20; b := 3; c := a mod b; {2}end;
name:
//Name關鍵字用於指出方法的別名, //對於一個要被外部引用的方法, 建議用Name申請方法別名, 以避免外部程序改動方法的實體內容.//從外部引用一個方法時, 如果方法有別名, 則必須用Name進行標識.function MessageBox(HWnd: Integer; Text, Caption: PChar; Flags: Integer): Integer; stdcall; external 'user32.dll' name 'MessageBoxA';
near:
//Near標示了函數呼叫協定, 指出函數可以被本地呼叫.//其他程式可以用dll的形式呼叫程式內的函數.它是向下相容的.function Add(a,b: Integer): Integer; near;//如果這個程式被編譯為Demo.exe, 且另一個處於本機的程式需要呼叫這個函式, 可以使用以下語句:function Add(a,b: Integer): Integer; stdcall; external 'Demo.exe';
nil:
//Nil用來表示一個空指標, 或是沒有實例的物件.while Node <> nil dobegin ListBox1.Items.Add(Node.Text); Node := Node.GetNext;end;
nodefault:
//NoDefault關鍵字指出了一個屬性不允許有預設值, 這通常用在繼承中.type TClassA = class private fValue: Integer; published property Value: Integer read fValue write fValue default 0; end; TClassB = class(TClassA ) published property Value:Integer read fValue write fValue nodefault; end;//由上例可知, TClassA中的Value有預設值0, //TClassB繼承了TClassA, 所以也繼承了其預設值, 在此用NoDefault去掉預設值
not:
//Not用於取反, 它否定了原先的結果.例如:if a > b then//可以寫成:if not(a < b) then//Not關鍵字通常用於切換Boolean型的屬性procedure Button1Click (Sender: TObject);begin StatusBar1.Visible := not StatusBar1.Visible;end;
object:
//Object用來宣告一個物件, 這個物件可以是任意的, 並且向下相容.Object只能被Object所繼承.//宣告物件的方法與宣告類別的方法是相同的.type ODemoA = object end; ODemoB = object(ODemoA) end;//Object關鍵字也用於宣告動態函數或過程, 例如:type TMyFun = function(i: Integer): Integer of Object; TMyProc = procedure(s: string) of object;//經過object宣告的函數或程序可以被動態的綁定到指定的函數體, 或是綁定到控制項是事件中.
of:
//Of關鍵用於和其他關鍵字構成指定的結構.Of可以與Case, Class, Array, File, Set, Object連用.//Case語句:case Tag Of 0: Result := 'a'; 1: Result := 'b';end;//Class語句:type TDemo = class of TComponent;//Array結構:var MyInt: array of Integer;//File結構:var MyFile: file of Byte;//Set語句:type TCol = (cA,cB,cC); TCols = set of TCol;//Object結構:type MyFun = function(I: Integer) : Integer of Object;
on:
//On關鍵字用於異常處理, 指出發生的異常, 並獲取異常信息.try i := StrToInt(s);except on E: exception do ShowMessage(E.Message);end;
or:
//一、表示邏輯或if (a>0) 或 (b>0) then//二、表示位運算vara,b,c: Integer;beginc := (a 或 b);end;//使用Or表示邏輯時, Or左右的表達式必須用小括號括起, 以避免以生條件的衝突//如果在條件語句中使用Or, 則編輯器不知道用戶使用Or做什麼例如:if a>0 or b>0 then//編譯器可能會理解為:if a>(0 or b)>0 then//或if (a>0) or (b>0) then//但是實際編譯時, 編譯器會產生一個衝突, 報告錯誤//並且第一種可能包含了a>b>c的形式, 這在Delphi中不被支持//所以使用Or運算符時必須使用括號, 以區分左右的條件.//表示位運算時也必須加上括號,將Or以及左右參數括起.
out:
//Out關鍵字說明了方法參數的輸出方式, 一般的函數只能有一個回傳值, //使用Out可以在一個函數中傳回多個結果.//Out和var不同, Out是以傳回值的形式進行參數回傳, 而var是直接輸入一個參數的位址.procedure X(out i: Integer; out s: string);begin i := i * 2; s := s + 'abc';end;procedure TForm1.Button1Click(Sender: TObject);var i: Integer; s: string;begin i := 20; s := 'xxx'; X(i,s);end;
overload:
//Overload關鍵字指出了用於重載的方法, 重載即方法名稱相同, //但是參數數量, 類型或順序不同, 滿足此條件的構成重載.function X(i: Integer): string; overload;function X(s: string): string; overload;//從父類別繼承時, 如果子類別擁有和父類別相同的方法, 則也必須用overload構成重載, //但是此類重載也必須滿足重載的要求.type TDemo = class(TComponent) public procedure CreateWnd(AOwner: TWinControl); overload; end;//如上例, 子類別擁有的方法為:procedure CreateWnd; {繼承自父類別}procedure CreateWnd(AOwner: TWinControl); {子類別宣告}//共兩個CreateWnd方法.//如果不使用重載, 則在子類別中可以覆寫父類別的方法.
override:
//Override用來覆寫一個Virtual或是Dynamic形式的方法.//覆蓋時必須沿用被覆寫方法的宣告, 並且不允許修改原方法的參數和回傳類型.procedure Create(AOwner: TComponent); override;/ /Override多用於繼承, 用子類別覆寫父類別的方法.type TClassA = class procedure X; virtual; end; TClassB = class(TClassA) procedure X; override; end;//如上例, 子類擁有的方法為:procedure X; {從父類覆蓋}//父類擁有的方法為:procedure X; {父類自身方法, 未被覆蓋} //如果父類別的方法未用Virtual或Dynamic宣告, //或是有修改參數的需要, 則必須用Reintroduce關鍵字進行覆蓋.
package:
//Package關鍵字用於指出一個工程為控制項庫.//控制項庫編譯後產生BPL檔, 可被安裝到Delphi的控制項庫中, 從而在以後的開發中使用控制項.package DATAX; requires rtl, clx ; contains MyUnit in 'C:/MyProject/MyUnit.pas';end.
packed:
//Packed關鍵字用於對結構體記錄或陣列進行打包, 打包後被打包物件的體積能顯著減小.type TPerson = packed Record PName: string[32]; PAge: Integer; end; MyArray: packed array of PChar;
pascal:
//Pascal標明了函數呼叫協定, //指出函數在呼叫時遵循Pascal原因, 即先對所有的變數進行初始化, //避免因非同步執行緒呼叫而產生的錯誤.它是向下相容的.function X (i: Integer): Integer; Pascal;begin Result := i * 2;end;
private:
//Private標示了類別內元素的存取區分權限, 被Private區分的元素只能被本類別內部存取.
procedure:
//Procedure用於宣告過程procedureX(i: Integer);//它也可以用於動態函數的宣告type TProc = procedure(i: Integer) of object;//動態宣告時, 不需要指出過程名稱, 只需要指出參數就可以, 具體的過程名可以在後期綁定.
program:
//Program關鍵字用來指出一個工程為應用程式.控制項庫編譯後產生exe檔, 可以直接執行program Project1;uses Forms, Unit1 in 'Unit1.pas' ;{$R *.res}begin Application.Initialize ; Application.CreateForm(TForm1, Form1); Application.Run;end.
property:
//Property關鍵字用於宣告屬性, 屬性分為顯式屬性和隱式屬性兩種, //只有聲明在published存取區分符下的屬性才是顯式屬性, 可以直接在物件檢視器中檢視. type TDemo = class Private fValue: Integr; Published property Value: Integer read fValue write fValue; end;//事件也是屬性的一種, 可以在published區分符下用Property進行宣告type TOnTextChange=procedure (Sender: TObject) of object; TDemo = class private fEvent: TOnTexChange; published property OntextChange: TOnTextChange read fEvent write fEvent; end;
protected:
//Protected標示了類別內元素的存取區分權限, 被Protected區分的元素只能被本類別內部和其子類別存取.
public:
//Public標明了類別內元素的存取區分權限, 被Public區分的元素能夠被類別內和類別外任何物件存取.
published:
//Published標明了類內元素的存取區分權限.//被Published區分的元素能夠被類內和類外任何RTTI對象訪問//只在聲明在Published區分符下的屬性才能夠成為顯式屬性並在物件檢視器中顯示.
raise:
//Raise語句用於拋出異常, //如果希望透過外部程式處理異常, 或是在異常發生時重新將異常拋出, 可以使用Raise語句.function GetString(i: Integer): string;begin if i < 0 then raise exception.Create('Integer Cannot smaller than 0'); Result := IntToStr(i);end;//在例外處理中,可以重新拋出異常try i := StrToInt(s);except on E: exception do raise exception.Create(E.Message);end;
read:
//Read用於標識屬性中讀取所使用的成員或方法.private fValue: Integer;published property Value: Integer readfValue;//上例中即表明Value屬性的值從fValue成員上讀取.
readonly:
//ReadOnly關鍵字用於識別一個物件是否只讀.propertyReadOnly;//當ReadOnly設為True時, 不允許使用者手動修改屬性, 只能透過其他物件來操作.
record:
//Record關鍵字用來宣告一個結構體記錄, //一個結構體可以視為一個不需要實例化的物件, 擁有自己的成員.type TPerson = record PName: string[32]; PAge: Integer; end ;
register:
//Register標示了函數呼叫協定, 指出函數在被呼叫時可以在註冊表內留下記錄.它是向下相容的.functionAdd(a,b: Integer): Integer; Register; Register//關鍵字還用於向控制項庫或是IDE註冊控製或是專家工具.procedure Register;begin RegisterComponents('Sample', [TDemo]);end;
reintroduce:
//Reintroduce用於重新發布方法, 通常用於繼承時, //如果要覆蓋的方法是靜態方法, 或是需要修改方法的參數等, 必須用Reintroduce進行重發布.//對於Virtual或Dynamic方法,可以直接用Override進行覆蓋.type TClassA = class procedure X; end; TClassB = class(TClassA) procedure X; reintroduce; end; TClassC = class(TClassB) procedure X(i: Integer); reintroduce; end;
repeat:
//repeat關鍵字用於引出repeat循環結構, //該循環必須先執行一次循環體, 然後再對循環條件進行判斷.repeat必須與Until關鍵字聯合使用.i := 0;repeat sum := sum + i; Inc(i);until(i >= 100);
requires:
//Requires關鍵字指出了編譯Package時的必備條件.若Requires的條件未滿足, 則不允許編譯包.package DATAX; requires rtl, clx;end.
resourcestring:
//ResourceString用來宣告資源字串, 資源字串可以在被宣告的結構內使用.ResourceString CreateError = 'Cannot create file %s'; OpenError = 'Cannot open file %s'; LineTooLong = 'Line too long' ; ProductName = 'Borland Rocks'; SomeResourceString = SomeTrueConstant;
safecall:
//Safecall是函數呼叫協定的一種, 它規定了被COM調用的函數所必須遵守和規則.//在編譯時, Safecall聲明的函數被編譯成COM接口兼容的.procedure X(s: WideString) ; safecall;//在編譯後變成:procedure X(s: PAnsiString);
set:
//Set關鍵字用於聲明集合類別, 集合類別允許用集合運算子, 如in等進行操作.type TCol = (cA,cB,cC); TCols = set ofTCol;//操作時允許使用加減符號來新增或刪除某個集合元素var Cols: Tcols;begin Cols := Cols + [cA,cB];end;
shl:
//SHL表示向左移位, 左移的位數即乘以2的冪數var x: Integer;begin X := 2 shl 3; {16}end;
shr:
//SHR表示向右移位, 右移的位數即除以2的冪數var x: Integer;begin X := 16 shr 2; {4}end;
stdcall:
//Stdcall是函數呼叫協定的一種, 它規定了能讓程式呼叫的函數所應遵守的規則.//Stdcall關鍵字必須在主調方和被調方之間形成配對.//例如, 被調方函數:Library Demo;function X(i: Integer): Integer; stdcall;begin Result := i * 2;end;exports X;beginend.//主調函數:function X(i: Integer): Integer; stdcall; external 'Demo.dll';//同時需要注意, 使用Stdcall關鍵字時, 被調函數是大小寫敏感的, 此處極容易出錯.
stored:
//Stored用來指出屬性的值是否能保留, 若指定了True, 則允許對屬性值進行賦值撤銷的操作.property Value: string read fValue write fValue stored True;
string:
//String是資料型別, 它代表了字串.var Str: string;
then:
//Then關鍵字用於If語句中, 當If條件成立時, 執行Then後的語句.var a,b: Integer;begin if a > b then WriteLn('a') else WriteLn('b'); end;
threadvar:
//Threadvar標識了一個隨執行緒啟動而建立的變數, //如果用Threadvar宣告變數, 則在程式結束前必須手動釋放其佔用的空間.threadvar S: AnsiString;S := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';S := '';//S := ''; 即釋放變數S所佔用的記憶體.
to:
//To關鍵字用於For語句, 指明循環變數是遞增的.for i := 10 to 100 do ListBox1.Items.Add(IntToStr(i));//在For語句中, 循環變數遞增用To關鍵字, 遞減用DownTo關鍵字.
try:
//try語句用於異常處理, 對於有可能發生異常的語句, 可以放在try結構下, 以便對其進行異常保護.try i := StrToInt(s);except ShowMessage('Error');end;
type:
//Type關鍵字用來宣告各種物件, 用Type關鍵字宣告的物件, 在傳遞時按引用傳遞.type TDemo = class end;//type也用來宣告枚舉類型或是按引用傳遞的變數.type TCol = (cA,cB,cC); TInt = Integer;
unit:
//Unit標識了單元的開頭, 單元的基本結構為Unit...Interface...implementation...end.Unit Unit1;Interface uses Classes;implementationend.//一個完整的單元必須擁有Unit作為開頭.
until:
//Until關鍵字用來判斷repeat循環結構的循環條件, //如果循環條件為真, 則退出循環.Until必須與repeat關鍵字聯合使用.i := 0;repeat sum := sum + i; Inc (i);until(i >= 100);
uses:
//Uses用來引用一個外部的單元, 並且能夠使用該單元中的公共部分.//Uses語句通常放在一個單元的接口或是實現部分.Interface uses Classes;Implemention uses frmAbout;
var:
//var關鍵字用來宣告一個變數或是物件, 用var宣告的變數接值傳遞.var i: Integer; s: string;//var也可以用來識別以參考傳遞的方法參數function X(var i: Integer): Integer;//上述函數中的參數i即按引用傳遞, 它的值可以在函數執行時被改變, 並返回主調函數.
varargs:
//varArgs標識了引用參數, 它必須和Cdecl關鍵字聯用, 表示允許呼叫的函數使用引用傳遞.function printf(Format: PChar): Integer; cdecl; varargs;//上述程式碼從C++的類別庫中引用了Printf函數, 並允許按引用的方式傳入參數.
virtual:
//Virtual用來宣告一個虛擬方法, //虛方法可以被覆寫, 並且可以使程式運行速度盡可能的快(區別於Dynamic).procedure X(i: Integer); virtual;
while:
//While關鍵字用於引出While循環語句, 循環前先進行循環條件的判斷, 如果條件為真則執行循環.i := 0;while i < 100 dobegin sum := sum + i; Inc(i) ;end;
with:
//With關鍵字用於將相同的物件集合起來處理, 它可以省去輸入大量重複的程式碼, 使程式碼看上去比較精簡.with Form1.Memo1.Lines dobegin Clear; Append('abc'); Append( 'def'); SaveToFile('C:/demo.txt');end;//上面這段程式碼如果不使用With語句,則顯得非常冗餘複製內容到剪貼簿代碼:Form1.Memo1.Lines.Clear;Form1.Memo1.Lines.Append('abc');Form1.Memo1.Lines.Append('def');Form1.Memo1. Lines.SaveToFile('C:/demo.txt');
write:
//Write用於標識屬性中寫入所使用的成員或方法.private fValue: Integer;published property Value: Integer writefValue;//上例中即表明Value屬性的值寫入到fValue成員上.
writeonly:
//writeonly關鍵字用來識別一個物件是否只寫.property writeonly;//當writeonly設為True時, 不允許使用者讀取屬性, 只能透過其他物件來操作.
xor:
//Xor用於取異或, 當兩個運算元相等時, 傳回False, 不等時回傳True.var a,b: Integer;begin a := 2; b := 3; if a xor b then WriteLn ('a xor b') else WriteLn('a not xor b');end;//Xor也用來計算異或值WriteLn(IntToStr(3 xor 5)); {6}