======================================================
注:本文源代码点此下载
======================================================
delphi 关键字详解[整理于 "橙子" 的帖子]
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
cmpedx, 32
jbe@a1
xoredx, edx
@a1: push esi
movesi, esp
subesp, 32
push ecx
movecx, 16
call cvtint
movedx, esi
popeax
call system.@lstrfrompcharlen
addesp, 32
popesi
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;
pushesi
movesi, esp
subesp, 16
xorecx, ecx
pushedx
xoredx, edx
callcvtint
movedx, esi
popeax
callsystem.@lstrfrompcharlen
addesp, 16
popesi
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 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}
绿色通道:好文要顶关注我收藏该文与我联系
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/