absolute
//它使得你能夠創建一個新變量, 並且該變量的起始地址與另一個變量相同.
var
Str: string[32];
StrLen: Byte absolute Str;
//這個聲明指定了變量StrLen起始地址與Str相同.
//由於字符串的第0個位置保存了字符串的長度, 所以StrLen的值即字符串長度.
begin
Str := '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
//二、表示位運算
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
//但是實際編譯時, 編譯器會產生一個衝突, 報告錯誤.
//並且第一種可能包含了a>b>c的形式, 這在Delphi中不被支持.
//所以使用And運算符時必須使用括號, 以區分左右的條件.
//表示位運算時也必須加上括號, 將And以及左右參數括起.
array
//Array用於表示數組, 任何的對象都能被聲明成數組.數組分爲靜態和動態的2種.
//靜態數組
var
Arr1: array [1..10] of Integer;
//動態數組, 由於聲明時不知其元素個數, 所以必須在後期用SetLength方法設置數組的大小
var
Arr2: array of Integer;
//數組作爲參數時, 不能傳入數組的大小, 只能傳入數組名, 然後用Length方法獲取數組的元素個數
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用於將一個對象轉換爲另一個對象
procedure BtnClick(Sender:TObject);
begin
(Sender as TButton).Caption := 'Clicked';
end;
//對於對象填充接口的轉換, 必須用As進行
(HTTPRIO as IExp).GetConnection;
//As不能用於數據類型的轉換, 下面的代碼是錯誤的:
var
i: Integer;
s: string;
begin
s := (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 ESI
end;
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做了修改, 變成
type
TDemo = class
automated
Str: 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 do
begin
sum := 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聲明方法, 則該方法在類與相關類中都可以使用, 譬如:
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方法作爲自身的方法進行調用.
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;
begin
fValue := 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[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運算的兩個數值必須均爲整型, 其運算結果也爲整型.
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 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關鍵字用於For語句, 指明循環變量是遞減的.
for i := 100 downto 1 do
ListBox1.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 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用於結束一個語句塊或是一個單元.
//它可以與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;
requires
rtl,
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用於輸出對象, 它必須被用在接口和實現之間, 可以同時輸出多個項, 項與項之間用逗號分開.
library Demo;
function X(i: Integer): string; stdcall;
begin
Result:=IntToStr(i);
end;
exports
X;
begin
end.
//如果輸出的對象被重載, 則必須給對象起個別名, 並註明參數.
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關鍵字用於引用一個外部的或是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和文件類型, 則文件可以被定義爲讀寫指定類型數據.
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 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;
begin
Result := i * 2;
end;
procedure Y;
begin
WriteLn(s);
end;
//用Forward前置聲明的方法在實現時不需要再輸入方法的參數和返回值, 直接使用方法名即可.
function
//Function用於聲明函數
function X(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 then
begin
WriteLn('a>b');
WriteLn('a=' + IntToStr(a));
WriteLn('b=' + IntToStr(b));
End
else
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聲明後, 可以在類聲明時指出接口中方法的實體, 如上例中的:
procedure IMyInterface.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(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關鍵字也用於在屬性中指出多個元素, 例如:
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
inherited Create(AOwner);
end;
//如果調用的是與自身同名的方法, 則也可以省去方法名和參數.如上例中的
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 ESI
end;
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); message ID_REFRESH;
procedure Refresh(var Msg: TMessageRecordtype);
begin
if Chr(Msg.Code) = #13 then
...
else
inherited;
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 do
begin
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) 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
//但是實際編譯時, 編譯器會產生一個衝突, 報告錯誤
//並且第一種可能包含了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用於聲明過程
procedure X(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 read fValue;
//上例中即表明Value屬性的值從fValue成員上讀取.
readonly
//ReadOnly關鍵字用於標識一個對象是否只讀.
property ReadOnly;
//當ReadOnly設爲True時, 不允許用戶手動修改屬性, 只能通過其他對象來操作.
record
//Record關鍵字用於聲明一個結構體記錄,
//一個結構體可以視爲一個不需要實例化的對象, 擁有自己的成員.
type
TPerson = record
PName: string[32];
PAge: Integer;
end;
register
//Register標明瞭函數調用協定, 指出函數在被調用時可以在註冊表內留下記錄.它是向下兼容的.
function Add(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 of TCol;
//操作時允許使用加減符號來添加或刪除某個集合元素
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;
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是一個數據類型, 它代表了字符串.
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;
implementation
end.
//一個完整的單元必須擁有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 do
begin
sum := sum + i;
Inc(i);
end;
with
//With關鍵字用於將相同的對象集合起來處理, 它可以省去輸入大量重複的代碼, 使代碼看上去比較精簡.
with Form1.Memo1.Lines do
begin
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 write fValue;
//上例中即表明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}
Delphi 關鍵字詳解
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.