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

Delphi常用关键字用法详解

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

absolute:

1
2
3
4
5
6
7
8
9
10
//它使得你能够创建一个新变量, 并且该变量的起始地址与另一个变量相同.
var
Str: string[32];
StrLen: Byte absoluteStr;
//这个声明指定了变量StrLen起始地址与Str相同.
//由于字符串的第0个位置保存了字符串的长度, 所以StrLen的值即字符串长度.
begin
Str := 'abc';
Edit1.Text := IntToStr(StrLen);
end;

abstract:

1
2
3
4
5
6
7
8
9
10
11
12
13
//它允许你创建抽象的方法, 包括有抽象方法的类称为抽象类.
//Abstract关键字必须与Virtual或Dynamic关键字同时使用, 因为抽象方法必须被覆盖式实现.
//抽象类不能实例化, 抽象方法不能包含方法体.
type
TDemo = class
private
protected
procedure X; virtual; abstract;
public
constructor Create;
destructor Destroy; override;
published
end;

and:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//一、表示逻辑与
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//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:

1
2
3
//Assembler关键字用于支持早期的汇编, 如80386等.
//它和Asm的区别:Asm允许使用Win32汇编, 而Assembler只允许80x86汇编, 它不允许Invoke语句的出现.
function IntToHex(AValue: Int64): string; assembler;

automated:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Automated访问区分符用于描述一个自动类型的成员, 它能够使程序的版本向下兼容.
//ComObj单元内的成员及其实例不能使用Automated访问区分符.
type
TDemo = class
automated
Str:WideString;
end;
//在程序的下一个版本中, 将Str做了修改, 变成
type
TDemo = class
automated
Str: AnsiString;
end
//则新版本的Str变量能够接受旧版本的WideString型数据, 并自动转换成AnsiString.
//在实际开发中, 如果没有特殊的需要, 一般不用automated访问区分符.

begin:

1
2
3
4
5
6
7
8
9
10
11
//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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//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:

1
2
3
4
5
6
7
8
9
//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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//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:

1
2
3
4
5
6
7
8
//Const关键字用于声明常量, 使用const声明的数据将不能在程序中被改变.
//也可以用来声明函数参数, 用const指定的参数不允许在函数中改变.
const MyFileName = 'Delphi';
const MyInteger = 100;
//用Const声明常量不需要指出其数据类型, 系统会自动判断类型, 并作自动调整.
//函数中可以用const声明不可更改的参数
function X(const i: Integer): string;
//此时在函数操作过程中, i的值不可改变.

constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
//constructor关键字用来声明一个类的构造函数, 当类被实例化时, 首先调用此函数
//构造函数一般用Create表示, Create方法能够连带类中存在的CreateWnd方法.
type
ClassDemo = class(TObject)
private
fValue: Integer;
public
constructor Create;
end;
constructor ClassDemo.Create;
begin
fValue := 0;
end;

contains:

1
2
3
4
5
6
7
8
//Contains关键字指出了某个包(Package)是否包含某个文件.
//用Contains引入的文件必须被添加到包文件中, 它可以避免关键文件的引用丢失.
package DATAX;
requires
rtl, clx;
contains
Db, DBLocal, DBXpress;
end.

default:

1
2
3
4
5
6
7
8
9
10
11
//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:

1
2
3
4
5
6
7
8
9
10
//Destructor用于标识析构函数, 析构函数在类被释放时自动调用.
//析构函数只允许覆盖, 再不允许重载.析构函数通常用Destroy作为函数名.
type
ClassDemo = class(TComponent)
public
destructor Destroy;override;
end;
//由于TComponent类中也有Destroy方法, 所以要将其重写
//但是若要重载析构函数, 则不允许, 下面代码是错误的:
destructor Destroy; overload;

dispid:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//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:

1
2
3
4
5
6
7
//DispInterface用于声明一个特定的适配器接口, 这个适配器能够接受标准系统接口中传入传出的数据.
//用DispInterface声明的接口不能被继承, 只能够被引用.
//DispInterface中方法只能调用, 并且必须被动态绑定.
//可以通过DispId为接口内方汉分配适配序号.
//DispInterface仅能用于Windows平台, 如果在Linux下进行开发, 则此关键字会自动被系统屏蔽.
//通常情况下, 不使用DispInterface.
//实例请参见DispId

div:

1
2
3
4
5
6
7
//Div用于求两数之整数商.用于Div运算的两个数值必须均为整型, 其运算结果也为整型.
var
a,b,c:Integer;
begin
a := 20; b := 3;
c := a div b; {6}
end;

do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Do关键字用于For, While, On, With语句, 构成特定的结构
//For语句:
for i := 1 to 100 do sum:=sum+i;
 
//While语句:
while i <

鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Matlab DIP(瓦)ch10图像分割练习发布时间:2022-07-18
下一篇:
MATLAB矩阵处理——2.1特殊矩阵 - Tea&amp;Honey发布时间: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