Delphi常用關(guān)鍵字用法詳解
本文詳細介紹了Delphi中常用的各個關(guān)鍵字名稱及用法,供大家在編程過程中借鑒參考之用。詳情如下:
absolute:
//它使得你能夠創(chuàng)建一個新變量, 并且該變量的起始地址與另一個變量相同. var Str: string[32]; StrLen: Byte absoluteStr; //這個聲明指定了變量StrLen起始地址與Str相同. //由于字符串的第0個位置保存了字符串的長度, 所以StrLen的值即字符串長度. begin Str := 'abc'; Edit1.Text := IntToStr(StrLen); end;
abstract:
//它允許你創(chuàng)建抽象的方法, 包括有抽象方法的類稱為抽象類. //Abstract關(guān)鍵字必須與Virtual或Dynamic關(guān)鍵字同時使用, 因為抽象方法必須被覆蓋式實現(xiàn). //抽象類不能實例化, 抽象方法不能包含方法體. 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 //二、表示位運算 var a,b,c: Integer; begin c := (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 //但是實際編譯時, 編譯器會產(chǎn)生一個沖突, 報告錯誤. //并且第一種可能包含了a>b>c的形式, 這在Delphi中不被支持. //所以使用And運算符時必須使用括號, 以區(qū)分左右的條件. //表示位運算時也必須加上括號, 將And以及左右參數(shù)括起.
array:
//Array用于表示數(shù)組, 任何的對象都能被聲明成數(shù)組.數(shù)組分為靜態(tài)和動態(tài)的2種. //靜態(tài)數(shù)組 var Arr1: array [1..10] of Integer; //動態(tài)數(shù)組, 由于聲明時不知其元素個數(shù), 所以必須在后期用SetLength方法設(shè)置數(shù)組的大小 var Arr2: array of Integer; //數(shù)組作為參數(shù)時, 不能傳入數(shù)組的大小, 只能傳入數(shù)組名, 然后用Length方法獲取數(shù)組的元素個數(shù) function X(A: array of Integer): Integer; var i: Integer; begin Result := 0; for i := 0 to Length(A)-1 do Result := Result + A[i]; end;
as:
//As用于將一個對象轉(zhuǎn)換為另一個對象 procedure BtnClick(Sender:TObject); begin (Sender as TButton).Caption := 'Clicked'; end; //對于對象填充接口的轉(zhuǎn)換, 必須用As進行 (HTTPRIO as IExp).GetConnection; //As不能用于數(shù)據(jù)類型的轉(zhuǎn)換, 下面的代碼是錯誤的: var i: Integer; s: string; begin s := (i as string); end; //正確寫法是: s := string(i);
asm:
//Asm關(guān)鍵字用于插入?yún)R編代碼, 使用匯編代碼時, 必須使用asm...end;的結(jié)構(gòu), 而非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 ESI end;
assembler:
//Assembler關(guān)鍵字用于支持早期的匯編, 如80386等. //它和Asm的區(qū)別:Asm允許使用Win32匯編, 而Assembler只允許80x86匯編, 它不允許Invoke語句的出現(xiàn). function IntToHex(AValue: Int64): string; assembler;
automated:
//Automated訪問區(qū)分符用于描述一個自動類型的成員, 它能夠使程序的版本向下兼容. //ComObj單元內(nèi)的成員及其實例不能使用Automated訪問區(qū)分符. type TDemo = class automated Str:WideString; end; //在程序的下一個版本中, 將Str做了修改, 變成 type TDemo = class automated Str: AnsiString; end //則新版本的Str變量能夠接受舊版本的WideString型數(shù)據(jù), 并自動轉(zhuǎn)換成AnsiString. //在實際開發(fā)中, 如果沒有特殊的需要, 一般不用automated訪問區(qū)分符.
begin:
//begin關(guān)鍵字用于表示一段程序或一個結(jié)構(gòu)的開始, 必須用end關(guān)鍵字來結(jié)束. procedure X; begin ShowMessage('A Demo'); end; //一般的結(jié)構(gòu), 如If, For, While等也需要用begin關(guān)鍵字來標(biāo)出結(jié)構(gòu)起始點 for i:=1 to 100 do begin sum := sum + i; if sum > 1000 then Break; end;
case:
//Case語句用于完成條件選擇, Case語句的的被選擇對象必須是有序類型, 包括整型, 枚舉類型, 字符型等. //Case語句必須由end結(jié)束,如果沒有相符合的選擇項, 可以加入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是函數(shù)調(diào)用協(xié)定的一種, 它規(guī)定了從C或C++編寫的DLL中調(diào)用函數(shù)所必須遵守的規(guī)則. //它可以將C或C++中的數(shù)據(jù)類型轉(zhuǎn)換為Delphi的. //例如C++中的代碼: int X(int i) { return i*2; } //這個函數(shù)被編譯在Demo.dll中, 用Delphi調(diào)用時必須使用: function X(i: Integer): Integer; Cdecl; external 'Demo.dll';
class:
//Class關(guān)鍵字用于聲明或繼承一個類, 也可以使類和接口同時繼承. //另外, Class關(guān)鍵字也能用于聲明類通用方法, 使得父類可以從類內(nèi)訪問子類的方法. type ClassDemo = class(TObject) private public constructor Create; end; //如果用class聲明方法, 則該方法在類與相關(guān)類中都可以使用, 譬如: type ClassA = class private public procedure Y; end; type ClassB = class(ClassA) private public class procedure X; end; //則在使用時ClassA能夠直接訪問ClassB的X方法 procedure ClassA.Y; begin Self.X; end; //此時父類將子類的class方法作為自身的方法進行調(diào)用.
const:
//Const關(guān)鍵字用于聲明常量, 使用const聲明的數(shù)據(jù)將不能在程序中被改變. //也可以用來聲明函數(shù)參數(shù), 用const指定的參數(shù)不允許在函數(shù)中改變. const MyFileName = 'Delphi'; const MyInteger = 100; //用Const聲明常量不需要指出其數(shù)據(jù)類型, 系統(tǒng)會自動判斷類型, 并作自動調(diào)整. //函數(shù)中可以用const聲明不可更改的參數(shù) function X(const i: Integer): string; //此時在函數(shù)操作過程中, i的值不可改變.
constructor:
//constructor關(guān)鍵字用來聲明一個類的構(gòu)造函數(shù), 當(dāng)類被實例化時, 首先調(diào)用此函數(shù) //構(gòu)造函數(shù)一般用Create表示, Create方法能夠連帶類中存在的CreateWnd方法. type ClassDemo = class(TObject) private fValue: Integer; public constructor Create; end; constructor ClassDemo.Create; begin fValue := 0; end;
contains:
//Contains關(guān)鍵字指出了某個包(Package)是否包含某個文件. //用Contains引入的文件必須被添加到包文件中, 它可以避免關(guān)鍵文件的引用丟失. package DATAX; requires rtl, clx; contains Db, DBLocal, DBXpress; end.
default:
//Default關(guān)鍵字用于指出一個屬性的默認值 //只有有序類型的屬性才允許默認值的存在, 否則必須在構(gòu)造函數(shù)中初始化屬性值. 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用于標(biāo)識析構(gòu)函數(shù), 析構(gòu)函數(shù)在類被釋放時自動調(diào)用. //析構(gòu)函數(shù)只允許覆蓋, 再不允許重載.析構(gòu)函數(shù)通常用Destroy作為函數(shù)名. type ClassDemo = class(TComponent) public destructor Destroy;override; end; //由于TComponent類中也有Destroy方法, 所以要將其重寫 //但是若要重載析構(gòu)函數(shù), 則不允許, 下面代碼是錯誤的: destructor Destroy; overload;
dispid:
//DispId關(guān)鍵字被用在DispInterface接口中, 用于指定特定的適配序號. //在DispInterface接口中, 適配序號必須是唯一的, //如果不指定DispId, 則系統(tǒng)會自動分配適配序號給接口內(nèi)每一個方法. //可以通過適配序號訪問DispInterface接口中的方法. type IStringsDisp = dispinterface ['{EE05DFE2-5549-11D0-9EA9-0020AF3D82DA}'] property ControlDefault[Index: Integer]: Olevariant dispid 0; default; function Count: Integer; dispid 1; property Item[Index: Integer]: Olevariant dispid 2; procedure Remove(Index: Integer); dispid 3; procedure Clear; dispid 4; function Add(Item: Olevariant): Integer; dispid 5; function _NewEnum: IUnknown; dispid -4; end;
dispinterface:
//DispInterface用于聲明一個特定的適配器接口, 這個適配器能夠接受標(biāo)準(zhǔn)系統(tǒng)接口中傳入傳出的數(shù)據(jù). //用DispInterface聲明的接口不能被繼承, 只能夠被引用. //DispInterface中方法只能調(diào)用, 并且必須被動態(tài)綁定. //可以通過DispId為接口內(nèi)方漢分配適配序號. //DispInterface僅能用于Windows平臺, 如果在Linux下進行開發(fā), 則此關(guān)鍵字會自動被系統(tǒng)屏蔽. //通常情況下, 不使用DispInterface. //實例請參見DispId
div:
//Div用于求兩數(shù)之整數(shù)商.用于Div運算的兩個數(shù)值必須均為整型, 其運算結(jié)果也為整型. var a,b,c:Integer; begin a := 20; b := 3; c := a div b; {6} end;
do:
//Do關(guān)鍵字用于For, While, On, With語句, 構(gòu)成特定的結(jié)構(gòu) //For語句: for i := 1 to 100 do sum:=sum+i; //While語句: while i < 100 do begin sum := sum + i; Inc(i); end; //On語句(異常處理): try i := StrToInt(s); except on exception do ShowMessage('Error!'); end; //With語句: with Memo1.Lines do begin Clear; Append('abc'); Append('123'); end;
downto:
//DownTo關(guān)鍵字用于For語句, 指明循環(huán)變量是遞減的. for i := 100 downto 1 do ListBox1.Items.Add(IntToStr(i)); //在For語句中, 循環(huán)變量遞增用To關(guān)鍵字, 遞減用DownTo關(guān)鍵字.
dynamic:
//Dynamic用于聲明一個動態(tài)的方法, //動態(tài)方法可以被覆蓋, 并且可以使代碼大小盡可能的減少(區(qū)別于Virtual). procedure X(i: Integer); dynamic;
else:
//else用于引導(dǎo)程序的運行方向, 它可以與If, Case和On語句聯(lián)用, 當(dāng)條件不滿足時, 轉(zhuǎn)到else下運行 //If語句(在If語句中, else前不允許有分號): if a > b then c := a else c:=b; //Case語句: case Tag Of 1:Result:=1; 2:Result:=2; 3:Result:=3; else Result:=0; end; //On語句(異常處理): try i := StrToInt(s); Excpet on EZeroDivide do Result := 1; on EOverflow do Result := 2; else Result := 0; end;
end:
//End用于結(jié)束一個語句塊或是一個單元. //它可以與begin, Case, Class, Interface, Asm, Unit, Package等相匹配. //對于語句塊(局部結(jié)束), End后必須添加分號. //而對于單元或包(全局結(jié)束), end后必須添加句號. //在If語句中else關(guān)鍵字前的End后不允許添加符號. procedure X; begin with Button1 do begin if Button1.ShowHint then Button1.Caption := 'Hinted' else Button1.Caption := 'Not Hinted'; end; end; //在包內(nèi)使用End來結(jié)束: package DATAX; requires rtl, clx; contains Db, DBLocal, DBXpress; end.
except:
//except關(guān)鍵字用于異常處理, 必須用在try語句內(nèi), 如果發(fā)生異常, 則執(zhí)行except后的語句 try i := StrToInt(s); except ShowMessage('Error!'); end;
export:
//Export標(biāo)明了函數(shù)調(diào)用協(xié)定, 指出函數(shù)可以被輸出, 輸出的函數(shù)能被本地或遠程調(diào)用. //其他程序可以用dll的形式調(diào)用程序內(nèi)的函數(shù).它是向下兼容的. function Add(a,b: Integer): Integer; export; //如果這個程序被編譯為Demo.exe, 并且另一個程序需要調(diào)用這個函數(shù), 可以使用以下語句 function Add(a,b: Integer): Integer; stdcall; external 'Demo.exe';
exports:
//exports用于輸出對象, 它必須被用在接口和實現(xiàn)之間, 可以同時輸出多個項, 項與項之間用逗號分開. libraryDemo; function X(i: Integer): string; stdcall; begin Result:=IntToStr(i); end; exports X; begin end. //如果輸出的對象被重載, 則必須給對象起個別名, 并注明參數(shù). 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; exports X(i: Integer) name 'x1', X(s: string) name 'x2'; begin end.
external:
//External關(guān)鍵字用于引用一個外部的或是OBJ內(nèi)的方法. {$L Demo.OBJ} procedure X(i:Integer);external; //如果是從dll或外部程序中引用, 則可以使用以下代碼: function A(FileName: string): string; external 'Demo.dll'; //如果被引用的函數(shù)被重載, 則必須另外指出引用的名稱. 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關(guān)鍵字時, 必須注意大小寫, 否則將出現(xiàn)錯誤.
far:
//Far標(biāo)明了函數(shù)調(diào)用協(xié)定, 指出函數(shù)可以被遠程調(diào)用. //其他程序可以用dll的形式調(diào)用程序內(nèi)的函數(shù).它是向下兼容的. functionAdd(a,b: Integer): Integer; Far; //如果這個程序被編譯為Demo.exe, 并且另一個處于其他計算機的程序需要調(diào)用這個函數(shù), 可以使用以下語句: function Add(a,b: Integer): Integer; stdcall; external 'Demo.exe';
file:
//File關(guān)鍵字指出了文件操作類型, 文件必須被聲明為File, //如果在File后追加Of和文件類型, 則文件可以被定義為讀寫指定類型數(shù)據(jù). type TPerson = record PName: string[32]; PAge: Integer; end; var PFile: file of TPerson;
finalization:
//finalization關(guān)鍵字標(biāo)識了單元被釋放時所要調(diào)用的方法, //通常是釋放掉單元中不能自動釋放的對象, 也可以不用. //finalization最常用的情況是對OLE對象做反初始化. initialization ActiveX.OleInitialize(nil); finalization ActiveX.OleUninitialize;
finally:
//finally關(guān)鍵字指出了異常處理中最后必須要調(diào)用的方法, //不論是否發(fā)生異常, finally后的語句總是在try語句結(jié)束時執(zhí)行. try Node := Node.GetNext; Edit1.Text := Node.Text; finally Node := nil; end;
for:
//For關(guān)鍵字引出For循環(huán)結(jié)構(gòu), 用于做指定次數(shù)的循環(huán). for i := 1 to 100 dosum := sum + i; //如果循環(huán)變量是遞減的, 則可以用DownTo關(guān)鍵字 for i := 100 downto 1 do Inc(sum);
forward:
//Forward關(guān)鍵字用于方法的前置定義.只定義方法聲明, 然后在程序的后面對方法進行實現(xiàn). //這么做有利于代碼的可讀性, 可以將所有的聲明放在一起, 然后將所有的實現(xiàn)也放在一起. function X(i: Integer): Integer; forward; procedure Y(s: string); forward; ... function X; begin Result := i * 2; end; procedure Y; begin WriteLn(s); end; //用Forward前置聲明的方法在實現(xiàn)時不需要再輸入方法的參數(shù)和返回值, 直接使用方法名即可.
function:
//Function用于聲明函數(shù) functionX(i: Integer): Integer; //它也可以用于動態(tài)函數(shù)的聲明 type TFun = function(i: Integer): Integer of object; //動態(tài)聲明時, 不需要指出函數(shù)名, 只需要指出參數(shù)和返回類型就可以, 具體的函數(shù)名可以在后期綁定.
goto:
//Goto語句用在跳轉(zhuǎn)行號, 可以跳轉(zhuǎn)到當(dāng)前結(jié)構(gòu)層內(nèi)任意位置. //必須在聲明處用label關(guān)鍵字聲明行號. //由于Goto語句會破壞程序的結(jié)構(gòu), 不推薦使用. 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關(guān)鍵字引出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語句的通常結(jié)構(gòu)是If...Then...else, else語句也可以不要. //在If語句內(nèi)如果有多個子語句, 則必須用begin...End結(jié)構(gòu)進行區(qū)分. if a > b then begin WriteLn('a>b'); WriteLn('a=' + IntToStr(a)); WriteLn('b=' + IntToStr(b)); End else WriteLn('b>a');
implementation:
//Implementation標(biāo)識了單元中的實現(xiàn)部分, 單元的基本結(jié)構(gòu)為: //Unit...Interface...implementation...end. //函數(shù)體, 過程體等必須寫在implementation關(guān)鍵字后. //如果在implementation后引用對象, 則對象是非公開的, 僅能供單元自身使用. implementation uses frmAbout; begin FormAbout.Show; end; //一個完整的單元必須擁有implementation部分.
implements:
//Implements指出了一個屬性從接口繼承, 此時屬性被轉(zhuǎn)換成接口對象. //通過接口動態(tài)綁定屬性, 并動態(tài)的設(shè)定屬性值. 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聲明后, 可以在類聲明時指出接口中方法的實體, 如上例中的: procedure IMyInterface.P1 = MyP1;
in:
//In用于判斷一個集合中是否包含某個元素.被判斷的內(nèi)容必須是單個集合元素和一個集合的實例. 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也用于工程文件中, 用于標(biāo)識某個文件是否被工程所引用. Uses Unit1 in 'Unit1.pas'; //In可以被用在For語句中, 用于循環(huán)取出一個集合中的元素. var s: string; sl: TStringList; begin ... for s In sl do begin ShowMessage(s); end; end;
index:
//Index用于在屬性中標(biāo)識序號, 以便用相同的屬性方法(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(const Index: Integer): Longint; begin case Index of 0: result := self.Left; 1: Result := self.Top; 2: result := self.Width; 3: result := self.Height; end; end; //Index關(guān)鍵字也用于在屬性中指出多個元素, 例如: property Selected[Index: Integer]: Boolean read GetSelected write SetSelected;
inherited:
//Inherited用于調(diào)用父類的方法. type TDemo = class(TComponent) public constructor Create(AOwner: TComponent); override; end; constructor TDemo.Create(AOwner: TComponent); begin inherited Create(AOwner); end; //如果調(diào)用的是與自身同名的方法, 則也可以省去方法名和參數(shù).如上例中的 inherited Create(AOwner); //可以改成: Inherited;
initialization:
//initialization關(guān)鍵字標(biāo)識了單元被載入時所要調(diào)用的方法, //通常是初始化一些不能自動初始化的對象, 也可以不用. //initialization最常用的情況是對OLE對象做初始化. initialization ActiveX.OleInitialize(nil); finalization ActiveX.OleUninitialize;
inline:
//InLine關(guān)鍵字用于Asm或assembler結(jié)構(gòu)中, //用于指出該匯編語句是向下兼容的.它對于程序的編譯沒有任何影響. 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 ESI end;
interface:
//Interface標(biāo)識了單元中的接口部分, 單元的基本結(jié)構(gòu)為: //Unit...Interface...implementation...end. //函數(shù), 過程等的聲明必須寫在Interface關(guān)鍵字后. //如果在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關(guān)鍵字用于對象的判斷, 有某些情況下, 也可以作"As"使用. var Comp: TComponent; begin ... if Comp Is TEdit then (Comp as TEdit).Text := 'Edit'; end;
label:
//label關(guān)鍵字用于聲明行號標(biāo)簽, 以便用Goto進行轉(zhuǎn)向, 不推薦使用. 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關(guān)鍵字用于指出一個工程為類庫.類庫編譯后生成DLL文件, 可被其他程序調(diào)用. 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關(guān)鍵字用于聲明消息方法, //帶有Message的方法必須指出接收的消息類型, 并通過引用將消息傳入方法中, 以便進行處理. procedure Refresh(var Msg: TMessageRecordtype); messageID_REFRESH; procedure Refresh(var Msg: TMessageRecordtype); begin if Chr(Msg.Code) = #13 then ... else inherited; end; //用戶可以自定義消息, 自定義消息也能夠被Message接收, 并引發(fā)事件.
mod:
//Mod用于求兩數(shù)之整數(shù)模, 即余數(shù).用于Mod運算的兩個數(shù)值必須均為整型, 其運算結(jié)果也為整型. var a,b,c: Integer; begin a := 20; b := 3; c := a mod b; {2} end;
name:
//Name關(guān)鍵字用于指出方法的別名, //對于一個要被外部引用的方法, 建議用Name申請方法別名, 以避免外部程序改動方法的實體內(nèi)容. //從外部引用一個方法時, 如果該方法有別名, 則必須用Name進行標(biāo)識. function MessageBox(HWnd: Integer; Text, Caption: PChar; Flags: Integer): Integer; stdcall; external 'user32.dll' name 'MessageBoxA';
near:
//Near標(biāo)明了函數(shù)調(diào)用協(xié)定, 指出函數(shù)可以被本地調(diào)用. //其他程序可以用dll的形式調(diào)用程序內(nèi)的函數(shù).它是向下兼容的. function Add(a,b: Integer): Integer; near; //如果這個程序被編譯為Demo.exe, 并且另一個處于本地的程序需要調(diào)用這個函數(shù), 可以使用以下語句: function Add(a,b: Integer): Integer; stdcall; external 'Demo.exe';
nil:
//Nil用于表示一個空指針, 或是沒有實例的對象. while Node <> nil do begin ListBox1.Items.Add(Node.Text); Node := Node.GetNext; end;
nodefault:
//NoDefault關(guān)鍵字指出了一個屬性不允許有默認值, 這通常用在繼承中. 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用于取反, 它否定了原先的結(jié)果.例如: if a > b then //可以寫成: if not(a < b) then //Not關(guān)鍵字通常用于切換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關(guān)鍵字還用于聲明動態(tài)函數(shù)或過程, 例如: type TMyFun = function(i: Integer): Integer of Object; TMyProc = procedure(s: string) of object; //經(jīng)過object聲明的函數(shù)或過程可以被動態(tài)的綁定到指定的函數(shù)體, 或是綁定到控件是事件中.
of:
//Of關(guān)鍵用于和其他關(guān)鍵字構(gòu)成指定的結(jié)構(gòu).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結(jié)構(gòu): var MyInt: array of Integer; //File結(jié)構(gòu): var MyFile: file of Byte; //Set語句: type TCol = (cA,cB,cC); TCols = set of TCol; //Object結(jié)構(gòu): type MyFun = function(I: Integer): Integer of Object;
on:
//On關(guān)鍵字用于異常處理, 指出發(fā)生的異常, 并獲取異常信息. try i := StrToInt(s); except on E: exception do ShowMessage(E.Message); end;
or:
//一、表示邏輯或 if (a>0) or (b>0) then //二、表示位運算 var a,b,c: Integer; begin c := (a or 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 //但是實際編譯時, 編譯器會產(chǎn)生一個沖突, 報告錯誤 //并且第一種可能包含了a>b>c的形式, 這在Delphi中不被支持 //所以使用Or運算符時必須使用括號, 以區(qū)分左右的條件. //表示位運算時也必須加上括號, 將Or以及左右參數(shù)括起.
out:
//Out關(guān)鍵字說明了方法參數(shù)的輸出方式, 一般的函數(shù)只能有一個返回值, //使用Out可以在一個函數(shù)中返回多個結(jié)果. //Out和var不同, Out是以返回值的形式進行參數(shù)返回, 而var是直接輸入一個參數(shù)的地址. 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關(guān)鍵字指出了用于重載的方法, 重載即方法名相同, //但是參數(shù)數(shù)量, 類型或順序不同, 滿足此條件的構(gòu)成重載. function X(i: Integer): string; overload; function X(s: string): string; overload; //從父類繼承時, 如果子類擁有和父類相同的方法, 則也必須用overload構(gòu)成重載, //但是此類重載也必須滿足重載的要求. type TDemo = class(TComponent) public procedure CreateWnd(AOwner: TWinControl); overload; end; //如上例, 子類擁有的方法為: procedure CreateWnd; {繼承自父類} procedure CreateWnd(AOwner: TWinControl); {子類聲明} //共兩個CreateWnd方法. //如果不使用重載, 則在子類中可以覆蓋父類的方法.
override:
//Override用于覆蓋一個Virtual或是Dynamic形式的方法. //覆蓋時必須沿用被覆蓋方法的聲明, 并且不允許修改原方法的參數(shù)和返回類型. 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聲明, //或是有修改參數(shù)的需要, 則必須用Reintroduce關(guān)鍵字進行覆蓋.
package:
//Package關(guān)鍵字用于指出一個工程為控件庫. //控件庫編譯后生成BPL文件, 可被安裝到Delphi的控件庫中, 從而在以后的開發(fā)中使用控件. package DATAX; requires rtl, clx; contains MyUnit in 'C:\MyProject\MyUnit.pas'; end.
packed:
//Packed關(guān)鍵字用于對結(jié)構(gòu)體記錄或數(shù)組進行打包, 打包后被打包對象的體積能顯著減小. type TPerson = packed Record PName: string[32]; PAge: Integer; end; MyArray: packed array of PChar;
pascal:
//Pascal標(biāo)明了函數(shù)調(diào)用協(xié)定, //指出函數(shù)在調(diào)用時遵循Pascal原因, 即先對所有的變量進行初始化, //避免因異步線程調(diào)用而產(chǎn)生的錯誤.它是向下兼容的. function X(i: Integer): Integer; Pascal; begin Result := i * 2; end;
private:
//Private標(biāo)明了類內(nèi)元素的訪問區(qū)分權(quán)限, 被Private區(qū)分的元素只能被本類內(nèi)部訪問.
procedure:
//Procedure用于聲明過程 procedureX(i: Integer); //它也可以用于動態(tài)函數(shù)的聲明 type TProc = procedure(i: Integer) of object; //動態(tài)聲明時, 不需要指出過程名, 只需要指出參數(shù)就可以, 具體的過程名可以在后期綁定.
program:
//Program關(guān)鍵字用于指出一個工程為應(yīng)用程序.控件庫編譯后生成exe文件, 可以直接執(zhí)行 program Project1; uses Forms, Unit1 in 'Unit1.pas' ; {$R *.res} begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end.
property:
//Property關(guān)鍵字用于聲明屬性, 屬性分為顯式屬性和隱式屬性兩種, //只有聲明在published訪問區(qū)分符下的屬性才是顯式屬性, 可以直接在對象查看器中查看. type TDemo = class Private fValue: Integr; Published property Value: Integer read fValue write fValue; end; //事件也是屬性的一種, 可以在published區(qū)分符下用Property進行聲明 type TOnTextChange=procedure (Sender: TObject) of object; TDemo = class private fEvent: TOnTexChange; published property OntextChange: TOnTextChange read fEvent write fEvent; end;
protected:
//Protected標(biāo)明了類內(nèi)元素的訪問區(qū)分權(quán)限, 被Protected區(qū)分的元素只能被本類內(nèi)部和其子類訪問.
public:
//Public標(biāo)明了類內(nèi)元素的訪問區(qū)分權(quán)限, 被Public區(qū)分的元素能夠被類內(nèi)和類外任何對象訪問.
published:
//Published標(biāo)明了類內(nèi)元素的訪問區(qū)分權(quán)限. //被Published區(qū)分的元素能夠被類內(nèi)和類外任何RTTI對象訪問 //只在聲明在Published區(qū)分符下的屬性才能夠成為顯式屬性并在對象查看器中顯示.
raise:
//Raise語句用于拋出異常, //如果希望通過外部程序處理異常, 或是在異常發(fā)生時重新將異常拋出, 可以使用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用于標(biāo)識屬性中讀取所使用的成員或方法. private fValue: Integer; published property Value: Integer readfValue; //上例中即表明Value屬性的值從fValue成員上讀取.
readonly:
//ReadOnly關(guān)鍵字用于標(biāo)識一個對象是否只讀. propertyReadOnly; //當(dāng)ReadOnly設(shè)為True時, 不允許用戶手動修改屬性, 只能通過其他對象來操作.
record:
//Record關(guān)鍵字用于聲明一個結(jié)構(gòu)體記錄, //一個結(jié)構(gòu)體可以視為一個不需要實例化的對象, 擁有自己的成員. type TPerson = record PName: string[32]; PAge: Integer; end;
register:
//Register標(biāo)明了函數(shù)調(diào)用協(xié)定, 指出函數(shù)在被調(diào)用時可以在注冊表內(nèi)留下記錄.它是向下兼容的. functionAdd(a,b: Integer): Integer; Register; Register //關(guān)鍵字還用于向控件庫或是IDE注冊控件或是專家工具. procedure Register; begin RegisterComponents('Sample', [TDemo]); end;
reintroduce:
//Reintroduce用于重新發(fā)布方法, 通常用于繼承時, //如果要覆蓋的方法是靜態(tài)方法, 或是需要修改方法的參數(shù)等, 必須用Reintroduce進行重發(fā)布. //對于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關(guān)鍵字用于引出repeat循環(huán)結(jié)構(gòu), //該循環(huán)必須先執(zhí)行一次循環(huán)體, 然后再對循環(huán)條件進行判斷.repeat必須與Until關(guān)鍵字聯(lián)合使用. i := 0; repeat sum := sum + i; Inc(i); until(i >= 100);
requires:
//Requires關(guān)鍵字指出了編譯Package時的必備條件.若Requires的條件未滿足, 則不允許編譯包. package DATAX; requires rtl, clx; end.
resourcestring:
//ResourceString用于聲明資源字符串, 資源字符串可以在被聲明的結(jié)構(gòu)內(nèi)使用. ResourceString CreateError = 'Cannot create file %s'; OpenError = 'Cannot open file %s'; LineTooLong = 'Line too long'; ProductName = 'Borland Rocks'; SomeResourceString = SomeTrueConstant;
safecall:
//Safecall是函數(shù)調(diào)用協(xié)定的一種, 它規(guī)定了被COM調(diào)用的函數(shù)所必須遵守和規(guī)則. //在編譯時, Safecall聲明的函數(shù)被編譯成COM接口兼容的. procedure X(s: WideString); safecall; //在編譯后成為: procedure X(s: PAnsiString);
set:
//Set關(guān)鍵字用于聲明集合類, 集合類允許用集合運算符, 如in等進行操作. type TCol = (cA,cB,cC); TCols = set ofTCol; //操作時允許使用加減符號來添加或刪除某個集合元素 var Cols: Tcols; begin Cols := Cols + [cA,cB]; end;
shl:
//SHL表示向左移位, 左移的位數(shù)即乘以2的冪數(shù) var x: Integer; begin X := 2 shl 3; {16} end;
shr:
//SHR表示向右移位, 右移的位數(shù)即除以2的冪數(shù) var x: Integer; begin X := 16 shr 2; {4} end;
stdcall:
//Stdcall是函數(shù)調(diào)用協(xié)定的一種, 它規(guī)定了能讓程序調(diào)用的函數(shù)所應(yīng)遵守的規(guī)則. //Stdcall關(guān)鍵字必須在主調(diào)方和被調(diào)方之間形成配對. //例如, 被調(diào)方函數(shù): Library Demo; function X(i: Integer): Integer; stdcall; begin Result := i * 2; end; exports X; begin end. //主調(diào)方函數(shù): function X(i: Integer): Integer; stdcall; external 'Demo.dll'; //同時需要注意, 使用Stdcall關(guān)鍵字時, 被調(diào)函數(shù)是大小寫敏感的, 此處極容易出錯.
stored:
//Stored用于指出一個屬性的值是否能被保留, 若指定了True, 則允許對屬性值進行賦值撤銷的操作. property Value: string read fValue write fValue stored True;
string:
//String是一個數(shù)據(jù)類型, 它代表了字符串. var Str: string;
then:
//Then關(guān)鍵字用于If語句中, 當(dāng)If條件成立時, 執(zhí)行Then后的語句. var a,b: Integer; begin if a > b then WriteLn('a') else WriteLn('b'); end;
threadvar:
//Threadvar標(biāo)識了一個隨線程啟動而創(chuàng)建的變量, //如果用Threadvar聲明變量, 則在程序結(jié)束前必須手動釋放其占用的空間. threadvar S: AnsiString; S := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; S := ''; //S := ''; 即釋放變量S所占用的內(nèi)存.
to:
//To關(guān)鍵字用于For語句, 指明循環(huán)變量是遞增的. for i := 10 to 100 do ListBox1.Items.Add(IntToStr(i)); //在For語句中, 循環(huán)變量遞增用To關(guān)鍵字, 遞減用DownTo關(guān)鍵字.
try:
//try語句用于異常處理, 對于有可能發(fā)生異常的語句, 可以放在try結(jié)構(gòu)下, 以便對其進行異常保護. try i := StrToInt(s); except ShowMessage('Error'); end;
type:
//Type關(guān)鍵字用于聲明各種對象, 用Type關(guān)鍵字聲明的對象, 在傳遞時按引用傳遞. type TDemo = class end; //type也用來聲明枚舉類型或是按引用傳遞的變量. type TCol = (cA,cB,cC); TInt = Integer;
unit:
//Unit標(biāo)識了單元的開頭, 單元的基本結(jié)構(gòu)為 Unit...Interface...implementation...end. Unit Unit1; Interface uses Classes; implementation end. //一個完整的單元必須擁有Unit作為開頭.
until:
//Until關(guān)鍵字用于判斷repeat循環(huán)結(jié)構(gòu)的循環(huán)條件, //如果循環(huán)條件為真, 則退出循環(huán).Until必須與repeat關(guān)鍵字聯(lián)合使用. i := 0; repeat sum := sum + i; Inc(i); until(i >= 100);
uses:
//Uses用于引用一個外部的單元, 并且能夠使用該單元中的公共部分. //Uses語句通常放在一個單元的接口或是實現(xiàn)部分. Interface uses Classes; Implemention uses frmAbout;
var:
//var關(guān)鍵字用于聲明一個變量或是對象, 用var聲明的變量接值傳遞. var i: Integer; s: string; //var也可以用于標(biāo)識按引用傳遞的方法參數(shù) function X(var i: Integer): Integer; //上述函數(shù)中的參數(shù)i即按引用傳遞, 它的值可以在函數(shù)執(zhí)行時被改變, 并返回主調(diào)函數(shù).
varargs:
//varArgs標(biāo)識了引用參數(shù), 它必須和Cdecl關(guān)鍵字聯(lián)用, 表明允許調(diào)用的函數(shù)使用引用傳遞. function printf(Format: PChar): Integer; cdecl; varargs; //上述代碼從C++的類庫中引用了Printf函數(shù), 并允許按引用的方式傳入?yún)?shù).
virtual:
//Virtual用于聲明一個虛方法, //虛方法可以被覆蓋, 并且可以使程序運行速度盡可能的快(區(qū)別于Dynamic). procedure X(i: Integer); virtual;
while:
//While關(guān)鍵字用于引出While循環(huán)語句, 循環(huán)前先進行循環(huán)條件的判斷, 如果條件為真則執(zhí)行循環(huán). i := 0; while i < 100 do begin sum := sum + i; Inc(i); end;
with:
//With關(guān)鍵字用于將相同的對象集合起來處理, 它可以省去輸入大量重復(fù)的代碼, 使代碼看上去比較精簡. with Form1.Memo1.Lines do begin Clear; Append('abc'); Append('def'); SaveToFile('C:\demo.txt'); end; //上面這段代碼如果不使用With語句, 則顯得非常冗余復(fù)制內(nèi)容到剪貼板代碼: Form1.Memo1.Lines.Clear; Form1.Memo1.Lines.Append('abc'); Form1.Memo1.Lines.Append('def'); Form1.Memo1.Lines.SaveToFile('C:\demo.txt');
write:
//Write用于標(biāo)識屬性中寫入所使用的成員或方法. private fValue: Integer; published property Value: Integer writefValue; //上例中即表明Value屬性的值寫入到fValue成員上.
writeonly:
//writeonly關(guān)鍵字用于標(biāo)識一個對象是否只寫. property writeonly; //當(dāng)writeonly設(shè)為True時, 不允許用戶讀取屬性, 只能通過其他對象來操作.
xor:
//Xor用于取異或, 當(dāng)兩個操作數(shù)相等時, 返回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}
欄 目:Delphi
下一篇:Delphi編程常用快捷鍵大全
本文標(biāo)題:Delphi常用關(guān)鍵字用法詳解
本文地址:http://mengdiqiu.com.cn/a1/Delphi/8632.html
您可能感興趣的文章
- 01-10在Delphi實現(xiàn)在數(shù)據(jù)庫中存取圖像的圖文演示無錯
- 01-10delphi建立、讀取、存貯INI文件的方法《三》
- 01-10Delphi Command模式
- 01-10delphi 正弦曲線圖
- 01-10delphi建立、讀取、存貯INI文件的方法《二》
- 01-10插件管理框架 for Delphi(二)
- 01-10插件管理框架 for Delphi(一)
- 01-10Delphi中判斷文件是否為文本文件的函數(shù)
- 01-10delphi中一個值得大家來考慮的DLL問題
- 01-10初探Delphi中的插件編程


閱讀排行
本欄相關(guān)
- 01-10在Delphi實現(xiàn)在數(shù)據(jù)庫中存取圖像的圖
- 01-10delphi建立、讀取、存貯INI文件的方法
- 01-10delphi 正弦曲線圖
- 01-10Delphi Command模式
- 01-10delphi建立、讀取、存貯INI文件的方法
- 01-10插件管理框架 for Delphi(二)
- 01-10插件管理框架 for Delphi(一)
- 01-10Delphi中判斷文件是否為文本文件的函
- 01-10delphi中一個值得大家來考慮的DLL問題
- 01-10初探Delphi中的插件編程
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)
- 01-10delphi制作wav文件的方法
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery