• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

黄聪:Delphi 关键字详解[整理于 "橙子" 的帖子]

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
absolute
//它使得你能够创建一个新变量, 并且该变量的起始地址与另一个变量相同.
var
Str: string[32];
StrLen: Byte absoluteStr;

//这个声明指定了变量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用于输出对象, 它必须被用在接口和实现之间, 可以同时输出多个项, 项与项之间用逗号分开.
libraryDemo;

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的形式调用程序内的函数.它是向下兼容的.
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;
begin
WriteLn(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 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)


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab手写神经网络实现识别手写数字发布时间:2022-07-18
下一篇:
MATLAB 矩阵转化为灰度图发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap