Delphi 关键字详解[整理于 橙子 的帖子]
生活随笔
收集整理的這篇文章主要介紹了
Delphi 关键字详解[整理于 橙子 的帖子]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Delphi 關鍵字詳解[整理于 "橙子" 的帖子]
?
absolute //它使得你能夠創建一個新變量, 并且該變量的起始地址與另一個變量相同. varStr: string[32];StrLen: Byte absolute Str;//這個聲明指定了變量StrLen起始地址與Str相同. //由于字符串的第0個位置保存了字符串的長度, 所以StrLen的值即字符串長度. beginStr := 'abc';Edit1.Text := IntToStr(StrLen); end;?
abstract //它允許你創建抽象的方法, 包括有抽象方法的類稱為抽象類. //Abstract關鍵字必須與Virtual或Dynamic關鍵字同時使用, 因為抽象方法必須被覆蓋式實現. //抽象類不能實例化, 抽象方法不能包含方法體. typeTDemo = classprivateprotectedprocedure X; virtual; abstract;publicconstructor Create;destructor Destroy; override;publishedend;?
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; asmCMP EDX, 32JBE @A1xor EDX, EDX@A1: PUSH ESIMOV ESI, ESPSUB ESP, 32PUSH ECXMOV ECX, 16CALL CvtIntMOV EDX, ESIPOP EAXCALL System.@LStrFromPCharLenADD ESP, 32POP ESI end;?
assembler //Assembler關鍵字用于支持早期的匯編, 如80386等. //它和Asm的區別:Asm允許使用Win32匯編, 而Assembler只允許80x86匯編, 它不允許Invoke語句的出現. function IntToHex(AValue: Int64): string; assembler;?
automated //Automated訪問區分符用于描述一個自動類型的成員, 它能夠使程序的版本向下兼容. //ComObj單元內的成員及其實例不能使用Automated訪問區分符. typeTDemo = classautomatedStr:WideString;end;//在程序的下一個版本中, 將Str做了修改, 變成 typeTDemo = classautomatedStr: AnsiString;end //則新版本的Str變量能夠接受舊版本的WideString型數據, 并自動轉換成AnsiString. //在實際開發中, 如果沒有特殊的需要, 一般不用automated訪問區分符.?
begin //begin關鍵字用于表示一段程序或一個結構的開始, 必須用end關鍵字來結束. procedure X; beginShowMessage('A Demo'); end;//一般的結構, 如If, For, While等也需要用begin關鍵字來標出結構起始點 for i:=1 to 100 do beginsum := sum + i;if sum > 1000 then Break; end;?
case //Case語句用于完成條件選擇, Case語句的的被選擇對象必須是有序類型, 包括整型, 枚舉類型, 字符型等. //Case語句必須由end結束,如果沒有相符合的選擇項, 可以加入else來作出通用選擇. function GetDays(AYear,AMonth: Integer): Integer; begincase AMonth of1,3,5,7,8,10,12: Result := 31;4,6,9,11: Result := 30;2: beginif IsLeapYear(AYear) thenResult:=29elseResult:=28;end;elseResult:=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關鍵字也能用于聲明類通用方法, 使得父類可以從類內訪問子類的方法. typeClassDemo = class(TObject)privatepublicconstructor 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方法. typeClassDemo = class(TObject)privatefValue: Integer;publicconstructor Create;end;constructor ClassDemo.Create; beginfValue := 0; end;?
contains //Contains關鍵字指出了某個包(Package)是否包含某個文件. //用Contains引入的文件必須被添加到包文件中, 它可以避免關鍵文件的引用丟失. package DATAX;requiresrtl, clx;containsDb, DBLocal, DBXpress; end.?
default //Default關鍵字用于指出一個屬性的默認值 //只有有序類型的屬性才允許默認值的存在, 否則必須在構造函數中初始化屬性值. typeClassDemo = classprivatefValue: Integer;publishedproperty Value: Integer read fValue write fValue default 0;end;//它也可以指出一個類的默認屬性 property strings[Index: Integer]: string read GetString write PutString; Default;?
destructor //Destructor用于標識析構函數, 析構函數在類被釋放時自動調用. //析構函數只允許覆蓋, 再不允許重載.析構函數通常用Destroy作為函數名. typeClassDemo = class(TComponent)publicdestructor Destroy;override;end;//由于TComponent類中也有Destroy方法, 所以要將其重寫 //但是若要重載析構函數, 則不允許, 下面代碼是錯誤的: destructor Destroy; overload;?
dispid //DispId關鍵字被用在DispInterface接口中, 用于指定特定的適配序號. //在DispInterface接口中, 適配序號必須是唯一的, //如果不指定DispId, 則系統會自動分配適配序號給接口內每一個方法. //可以通過適配序號訪問DispInterface接口中的方法. typeIStringsDisp = 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用于聲明一個特定的適配器接口, 這個適配器能夠接受標準系統接口中傳入傳出的數據. //用DispInterface聲明的接口不能被繼承, 只能夠被引用. //DispInterface中方法只能調用, 并且必須被動態綁定. //可以通過DispId為接口內方漢分配適配序號. //DispInterface僅能用于Windows平臺, 如果在Linux下進行開發, 則此關鍵字會自動被系統屏蔽. //通常情況下, 不使用DispInterface. //實例請參見DispId?
div //Div用于求兩數之整數商.用于Div運算的兩個數值必須均為整型, 其運算結果也為整型. vara,b,c:Integer; begina := 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 do beginsum := sum + i;Inc(i); end;//On語句(異常處理): tryi := StrToInt(s); excepton exception do ShowMessage('Error!'); end;//With語句: with Memo1.Lines do beginClear;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 := a elsec:=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; beginwith Button1 dobeginif Button1.ShowHint thenButton1.Caption := 'Hinted'elseButton1.Caption := 'Not Hinted';end; end;//在包內使用End來結束: package DATAX;requiresrtl,clx;contains Db, DBLocal, DBXpress; end.?
except //except關鍵字用于異常處理, 必須用在try語句內, 如果發生異常, 則執行except后的語句 tryi := StrToInt(s); exceptShowMessage('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用于輸出對象, 它必須被用在接口和實現之間, 可以同時輸出多個項, 項與項之間用逗號分開. library Demo;function X(i: Integer): string; stdcall; beginResult:=IntToStr(i); end;exportsX;begin end.//如果輸出的對象被重載, 則必須給對象起個別名, 并注明參數. library Demo;function X(i: Integer): string; overload; stdcall; beginResult := IntToStr(i); end;function X(s: string): Integer; overload; stdcall; beginResult := StrToInt(s); end;exportsX(i: Integer) name 'x1',X(s: string) name 'x2';begin end.?
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的形式調用程序內的函數.它是向下兼容的. function Add(a,b: Integer): Integer; Far;//如果這個程序被編譯為Demo.exe, 并且另一個處于其他計算機的程序需要調用這個函數, 可以使用以下語句: function Add(a,b: Integer): Integer; stdcall; external 'Demo.exe';?
file //File關鍵字指出了文件操作類型, 文件必須被聲明為File, //如果在File后追加Of和文件類型, 則文件可以被定義為讀寫指定類型數據. typeTPerson = recordPName: string[32];PAge: Integer;end; varPFile: file of TPerson;?
finalization //finalization關鍵字標識了單元被釋放時所要調用的方法, //通常是釋放掉單元中不能自動釋放的對象, 也可以不用. //finalization最常用的情況是對OLE對象做反初始化. initializationActiveX.OleInitialize(nil); finalizationActiveX.OleUninitialize;?
finally //finally關鍵字指出了異常處理中最后必須要調用的方法, //不論是否發生異常, finally后的語句總是在try語句結束時執行. tryNode := Node.GetNext;Edit1.Text := Node.Text; finallyNode := nil; end;?
for //For關鍵字引出For循環結構, 用于做指定次數的循環. for i := 1 to 100 do sum := 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; beginResult := i * 2; end;procedure Y; beginWriteLn(s); end;//用Forward前置聲明的方法在實現時不需要再輸入方法的參數和返回值, 直接使用方法名即可.?
function //Function用于聲明函數 function X(i: Integer): Integer;//它也可以用于動態函數的聲明 typeTFun = function(i: Integer): Integer of object;//動態聲明時, 不需要指出函數名, 只需要指出參數和返回類型就可以, 具體的函數名可以在后期綁定.?
goto //Goto語句用在跳轉行號, 可以跳轉到當前結構層內任意位置. //必須在聲明處用label關鍵字聲明行號. //由于Goto語句會破壞程序的結構, 不推薦使用. vara,b: Integer; labelX,Y; beginif a > b thengoto Xelsegoto Y; X:WriteLn('a > b'); Y:WriteLn('b > a'); end;?
if //If關鍵字引出If條件語句, 用于對條件進行判斷. vara,b: Integer; begina := 2; b := 3;if a>b thenWriteLn('a=' + IntToStr(a))elseWriteLn('b=' + IntToStr(b)); end;//If語句的通常結構是If...Then...else, else語句也可以不要. //在If語句內如果有多個子語句, 則必須用begin...End結構進行區分. if a > b then beginWriteLn('a>b');WriteLn('a=' + IntToStr(a));WriteLn('b=' + IntToStr(b)); End elseWriteLn('b>a');?
implementation //Implementation標識了單元中的實現部分, 單元的基本結構為: //Unit...Interface...implementation...end. //函數體, 過程體等必須寫在implementation關鍵字后. //如果在implementation后引用對象, 則對象是非公開的, 僅能供單元自身使用. implementationuses frmAbout; beginFormAbout.Show; end;//一個完整的單元必須擁有implementation部分.?
implements //Implements指出了一個屬性從接口繼承, 此時屬性被轉換成接口對象. //通過接口動態綁定屬性, 并動態的設定屬性值. typeIMyInterface = interfaceprocedure P1;procedure P2;end;TMyImplclass = classprocedure 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用于判斷一個集合中是否包含某個元素.被判斷的內容必須是單個集合元素和一個集合的實例. typeTCol = (cA,cB,cC);TCols = set of TCol; varCols: TCols; beginCols := [cA,cB];if cA in Cols thenShowMessage('cA in Cols')elseShowMessage('cA not in Cols'); end;//In也用于工程文件中, 用于標識某個文件是否被工程所引用. UsesUnit1 in 'Unit1.pas';//In可以被用在For語句中, 用于循環取出一個集合中的元素. vars: string;sl: TStringList; begin...for s In sl dobeginShowMessage(s);end; end;?
index //Index用于在屬性中標識序號, 以便用相同的屬性方法(Get,Set)對不同的屬性進行操作. typeTForm1 = class(TForm)privatefunction GetInfo(const Index: Integer): Longint;procedure SetInfo(const Index: Integer; const Value: Longint);publicproperty 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; begincase Index of0: 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用于調用父類的方法. typeTDemo = class(TComponent)publicconstructor Create(AOwner: TComponent); override;end;constructor TDemo.Create(AOwner: TComponent); begininherited Create(AOwner); end;//如果調用的是與自身同名的方法, 則也可以省去方法名和參數.如上例中的 inherited Create(AOwner); //可以改成: Inherited;?
initialization //initialization關鍵字標識了單元被載入時所要調用的方法, //通常是初始化一些不能自動初始化的對象, 也可以不用. //initialization最常用的情況是對OLE對象做初始化. initializationActiveX.OleInitialize(nil); finalizationActiveX.OleUninitialize;?
inline //InLine關鍵字用于Asm或assembler結構中, //用于指出該匯編語句是向下兼容的.它對于程序的編譯沒有任何影響. function IntToStr(Value: Integer): string; asmInLine;PUSH ESIMOV ESI, ESPSUB ESP, 16xor ECX, ECXPUSH EDXxor EDX, EDXCALL CvtIntMOV EDX, ESIPOP EAXCALL System.@LStrFromPCharLenADD ESP, 16POP ESI end;?
interface //Interface標識了單元中的接口部分, 單元的基本結構為: //Unit...Interface...implementation...end. //函數, 過程等的聲明必須寫在Interface關鍵字后. //如果在Interface后引用對象, 則對象是沒有實例的, 使用時必須被實例化. Interfaceuses frmAbout; varFAbout: TFormAbout; beginFAbout := TFormAbout.Create(Self);FAbout.Show; end;//一個完整的單元必須擁有Interface部分. //Interface也可以用作接口的聲明. typeIMalloc = 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"使用. varComp: TComponent; begin...if Comp Is TEdit then(Comp as TEdit).Text := 'Edit'; end;?
label //label關鍵字用于聲明行號標簽, 以便用Goto進行轉向, 不推薦使用. vara,b: Integer; labelX,Y; beginif a > b thengoto Xelsegoto Y; X:WriteLn('a>b'); Y:WriteLn('b>a'); end;?
library //Library關鍵字用于指出一個工程為類庫.類庫編譯后生成DLL文件, 可被其他程序調用. library Editors; uses EdInit, EdInOut, EdFormat, EdPrint; exportsInitEditors,doneEditors name done,InsertText name Insert,DeleteSelection name Delete,FormatSelection,PrintSelection name Print,SetErrorHandler; beginInitLibrary; end.?
message //Message關鍵字用于聲明消息方法, //帶有Message的方法必須指出接收的消息類型, 并通過引用將消息傳入方法中, 以便進行處理. procedure Refresh(var Msg: TMessageRecordtype); message ID_REFRESH;procedure Refresh(var Msg: TMessageRecordtype); beginif Chr(Msg.Code) = #13 then...elseinherited; end;//用戶可以自定義消息, 自定義消息也能夠被Message接收, 并引發事件.?
mod //Mod用于求兩數之整數模, 即余數.用于Mod運算的兩個數值必須均為整型, 其運算結果也為整型. vara,b,c: Integer; begina := 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 do beginListBox1.Items.Add(Node.Text);Node := Node.GetNext; end;?
nodefault //NoDefault關鍵字指出了一個屬性不允許有默認值, 這通常用在繼承中. typeTClassA = classprivatefValue: Integer;publishedproperty Value: Integer read fValue write fValue default 0;end;TClassB = class(TClassA)publishedproperty 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); beginStatusBar1.Visible := not StatusBar1.Visible; end;?
object //Object用于聲明一個對象, 這個對象可以是任意的, 并且向下兼容.Object只能被Object所繼承. //聲明對象的方法與聲明類的方法是相同的. typeODemoA = objectend;ODemoB = object(ODemoA)end;//Object關鍵字還用于聲明動態函數或過程, 例如: typeTMyFun = 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 Of0: Result := 'a';1: Result := 'b'; end;//Class語句: typeTDemo = class of TComponent;//Array結構: varMyInt: array of Integer;//File結構: varMyFile: file of Byte;//Set語句: typeTCol = (cA,cB,cC);TCols = set of TCol;//Object結構: typeMyFun = function(I: Integer): Integer of Object;?
on //On關鍵字用于異常處理, 指出發生的異常, 并獲取異常信息. tryi := StrToInt(s); excepton E: exception doShowMessage(E.Message); end;?
or //一、表示邏輯或 if (a>0) or (b>0) then//二、表示位運算 vara,b,c: Integer; beginc := (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 //但是實際編譯時, 編譯器會產生一個沖突, 報告錯誤 //并且第一種可能包含了a>b>c的形式, 這在Delphi中不被支持 //所以使用Or運算符時必須使用括號, 以區分左右的條件. //表示位運算時也必須加上括號, 將Or以及左右參數括起.?
out //Out關鍵字說明了方法參數的輸出方式, 一般的函數只能有一個返回值, //使用Out可以在一個函數中返回多個結果. //Out和var不同, Out是以返回值的形式進行參數返回, 而var是直接輸入一個參數的地址. procedure X(out i: Integer; out s: string); begini := i * 2;s := s + 'abc'; end;procedure TForm1.Button1Click(Sender: TObject); vari: Integer;s: string; begini := 20;s := 'xxx';X(i,s); end;?
overload //Overload關鍵字指出了用于重載的方法, 重載即方法名相同, //但是參數數量, 類型或順序不同, 滿足此條件的構成重載. function X(i: Integer): string; overload; function X(s: string): string; overload;//從父類繼承時, 如果子類擁有和父類相同的方法, 則也必須用overload構成重載, //但是此類重載也必須滿足重載的要求. typeTDemo = class(TComponent)publicprocedure CreateWnd(AOwner: TWinControl); overload;end;//如上例, 子類擁有的方法為: procedure CreateWnd; {繼承自父類} procedure CreateWnd(AOwner: TWinControl); {子類聲明} //共兩個CreateWnd方法. //如果不使用重載, 則在子類中可以覆蓋父類的方法.?
override //Override用于覆蓋一個Virtual或是Dynamic形式的方法. //覆蓋時必須沿用被覆蓋方法的聲明, 并且不允許修改原方法的參數和返回類型. procedure Create(AOwner: TComponent); override;//Override多用于繼承, 用子類覆蓋掉父類的方法. typeTClassA = classprocedure X; virtual;end;TClassB = class(TClassA)procedure X; override;end;//如上例, 子類擁有的方法為: procedure X; {從父類覆蓋} //父類擁有的方法為: procedure X; {父類自身方法, 未被覆蓋}//如果父類的方法未用Virtual或Dynamic聲明, //或是有修改參數的需要, 則必須用Reintroduce關鍵字進行覆蓋.?
package //Package關鍵字用于指出一個工程為控件庫. //控件庫編譯后生成BPL文件, 可被安裝到Delphi的控件庫中, 從而在以后的開發中使用控件. package DATAX;requiresrtl,clx;containsMyUnit in 'C:/MyProject/MyUnit.pas'; end.?
packed //Packed關鍵字用于對結構體記錄或數組進行打包, 打包后被打包對象的體積能顯著減小. typeTPerson = packed RecordPName: string[32];PAge: Integer;end;MyArray: packed array of PChar;?
pascal //Pascal標明了函數調用協定, //指出函數在調用時遵循Pascal原因, 即先對所有的變量進行初始化, //避免因異步線程調用而產生的錯誤.它是向下兼容的. function X(i: Integer): Integer; Pascal; beginResult := i * 2; end;?
private //Private標明了類內元素的訪問區分權限, 被Private區分的元素只能被本類內部訪問.?
procedure //Procedure用于聲明過程 procedure X(i: Integer);//它也可以用于動態函數的聲明 typeTProc = procedure(i: Integer) of object;//動態聲明時, 不需要指出過程名, 只需要指出參數就可以, 具體的過程名可以在后期綁定.?
program //Program關鍵字用于指出一個工程為應用程序.控件庫編譯后生成exe文件, 可以直接執行 program Project1; usesForms,Unit1 in 'Unit1.pas' ; {$R *.res} beginApplication.Initialize;Application.CreateForm(TForm1, Form1);Application.Run; end.?
property //Property關鍵字用于聲明屬性, 屬性分為顯式屬性和隱式屬性兩種, //只有聲明在published訪問區分符下的屬性才是顯式屬性, 可以直接在對象查看器中查看. typeTDemo = classPrivatefValue: Integr;Publishedproperty Value: Integer read fValue write fValue;end;//事件也是屬性的一種, 可以在published區分符下用Property進行聲明 typeTOnTextChange=procedure (Sender: TObject) of object;TDemo = classprivatefEvent: TOnTexChange;publishedproperty 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; beginif i < 0 thenraise exception.Create('Integer Cannot smaller than 0');Result := IntToStr(i); end;//在異常處理中, 可以重新拋出異常 tryi := StrToInt(s); excepton E: exception doraise exception.Create(E.Message); end;?
read //Read用于標識屬性中讀取所使用的成員或方法. privatefValue: Integer; publishedproperty Value: Integer read fValue;//上例中即表明Value屬性的值從fValue成員上讀取.?
readonly //ReadOnly關鍵字用于標識一個對象是否只讀. property ReadOnly;//當ReadOnly設為True時, 不允許用戶手動修改屬性, 只能通過其他對象來操作.?
record //Record關鍵字用于聲明一個結構體記錄, //一個結構體可以視為一個不需要實例化的對象, 擁有自己的成員. typeTPerson = recordPName: string[32];PAge: Integer;end;?
register //Register標明了函數調用協定, 指出函數在被調用時可以在注冊表內留下記錄.它是向下兼容的. function Add(a,b: Integer): Integer; Register; Register//關鍵字還用于向控件庫或是IDE注冊控件或是專家工具. procedure Register; beginRegisterComponents('Sample', [TDemo]); end;?
reintroduce //Reintroduce用于重新發布方法, 通常用于繼承時, //如果要覆蓋的方法是靜態方法, 或是需要修改方法的參數等, 必須用Reintroduce進行重發布. //對于Virtual或Dynamic方法, 可以直接用Override進行覆蓋. typeTClassA = classprocedure 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; repeatsum := sum + i;Inc(i); until(i >= 100);?
requires //Requires關鍵字指出了編譯Package時的必備條件.若Requires的條件未滿足, 則不允許編譯包. package DATAX;requiresrtl,clx; end.?
resourcestring //ResourceString用于聲明資源字符串, 資源字符串可以在被聲明的結構內使用. ResourceStringCreateError = '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等進行操作. typeTCol = (cA,cB,cC);TCols = set of TCol;//操作時允許使用加減符號來添加或刪除某個集合元素 varCols: Tcols; beginCols := Cols + [cA,cB]; end;?
shl //SHL表示向左移位, 左移的位數即乘以2的冪數 varx: Integer; beginX := 2 shl 3; {16} end;?
shr //SHR表示向右移位, 右移的位數即除以2的冪數 varx: Integer; beginX := 16 shr 2; {4} end;?
stdcall //Stdcall是函數調用協定的一種, 它規定了能讓程序調用的函數所應遵守的規則. //Stdcall關鍵字必須在主調方和被調方之間形成配對. //例如, 被調方函數: Library Demo; function X(i: Integer): Integer; stdcall; beginResult := i * 2; end; exportsX; begin end.//主調方函數: function X(i: Integer): Integer; stdcall; external 'Demo.dll';//同時需要注意, 使用Stdcall關鍵字時, 被調函數是大小寫敏感的, 此處極容易出錯.?
stored //Stored用于指出一個屬性的值是否能被保留, 若指定了True, 則允許對屬性值進行賦值撤銷的操作. property Value: string read fValue write fValue stored True;?
string //String是一個數據類型, 它代表了字符串. varStr: string;?
then //Then關鍵字用于If語句中, 當If條件成立時, 執行Then后的語句. vara,b: Integer; beginif a > b thenWriteLn('a')elseWriteLn('b'); end;?
threadvar //Threadvar標識了一個隨線程啟動而創建的變量, //如果用Threadvar聲明變量, 則在程序結束前必須手動釋放其占用的空間. threadvar S: AnsiString; S := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; S := '';//S := ''; 即釋放變量S所占用的內存.?
to //To關鍵字用于For語句, 指明循環變量是遞增的. for i := 10 to 100 doListBox1.Items.Add(IntToStr(i));//在For語句中, 循環變量遞增用To關鍵字, 遞減用DownTo關鍵字.?
try //try語句用于異常處理, 對于有可能發生異常的語句, 可以放在try結構下, 以便對其進行異常保護. tryi := StrToInt(s); exceptShowMessage('Error'); end;?
type //Type關鍵字用于聲明各種對象, 用Type關鍵字聲明的對象, 在傳遞時按引用傳遞. typeTDemo = classend;//type也用來聲明枚舉類型或是按引用傳遞的變量. typeTCol = (cA,cB,cC);TInt = Integer;?
unit //Unit標識了單元的開頭, 單元的基本結構為 Unit...Interface...implementation...end. Unit Unit1; Interfaceuses Classes; implementation end.//一個完整的單元必須擁有Unit作為開頭.?
until //Until關鍵字用于判斷repeat循環結構的循環條件, //如果循環條件為真, 則退出循環.Until必須與repeat關鍵字聯合使用. i := 0; repeatsum := sum + i;Inc(i); until(i >= 100);?
uses //Uses用于引用一個外部的單元, 并且能夠使用該單元中的公共部分. //Uses語句通常放在一個單元的接口或是實現部分. Interfaceuses Classes; Implementionuses frmAbout;?
var //var關鍵字用于聲明一個變量或是對象, 用var聲明的變量接值傳遞. vari: 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 do beginsum := sum + i;Inc(i); end;?
with //With關鍵字用于將相同的對象集合起來處理, 它可以省去輸入大量重復的代碼, 使代碼看上去比較精簡. with Form1.Memo1.Lines do beginClear;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用于標識屬性中寫入所使用的成員或方法. privatefValue: Integer; publishedproperty Value: Integer write fValue;//上例中即表明Value屬性的值寫入到fValue成員上.?
writeonly //writeonly關鍵字用于標識一個對象是否只寫. property writeonly;//當writeonly設為True時, 不允許用戶讀取屬性, 只能通過其他對象來操作.?
xor //Xor用于取異或, 當兩個操作數相等時, 返回False, 不等時返回True. vara,b: Integer; begina := 2; b := 3;if a xor b thenWriteLn('a xor b')elseWriteLn('a not xor b'); end;//Xor也用于計算異或值 WriteLn(IntToStr(3 xor 5)); {6}總結
以上是生活随笔為你收集整理的Delphi 关键字详解[整理于 橙子 的帖子]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 作者:林春雨,男,现任北京拓尔思信息技术
- 下一篇: 作者:鄂世嘉,男,同济大学博士生,CCF