Delphi 关键字详解 (2)

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函数, 并允许按引用的方式传入参数.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值