comeheres 发表于 2015-1-31 23:33:24

Delphi 关键字详解

absolute
//它使得你能够创建一个新变量, 并且该变量的起始地址与另一个变量相同.   
var
    Str: string;
    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
//二、表示位运算vara,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 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;
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@A1xor
    EDX, 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: string read GetString write PutString; Default;
更多关键字详解,请查看附件
**** Hidden Message *****

拈花小仙 发表于 2015-2-1 01:23:57

{:7_139:}强烈支持大神哦~

tangm 发表于 2015-7-4 13:03:23

看看。

tiandiweicheng0 发表于 2015-7-8 15:10:00

学习了,谢谢

黑色传说 发表于 2015-7-12 16:52:22

顶楼主啦..希望楼主多发精品好帖啦.....

到鱼c学delphi 发表于 2015-7-13 20:17:09

thank you

看山不是山 发表于 2015-7-18 21:07:19

看看,。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
{:1_1:}

dreamertn9527 发表于 2015-10-22 20:38:40

感谢分享!

bowk 发表于 2015-11-7 09:25:41

感覺不錯
來看看

天晴0 发表于 2016-3-9 22:14:15

111

jyxzql 发表于 2016-4-14 18:51:06

赞一个....

gxlclj 发表于 2016-4-20 03:23:16


顶楼主啦..希望楼主多发精品好帖啦.....

狂奔的蜗牛兆 发表于 2016-4-22 15:32:27

234324

tlwangxd 发表于 2016-6-23 10:33:07

xuexi

xjg 发表于 2016-6-23 13:15:23

交管局回复

mack110 发表于 2016-6-26 16:00:44

看看

秋思 发表于 2016-7-28 18:06:11

感谢楼主

lll673368016 发表于 2016-10-8 20:52:53

property strings: string read GetString write PutString; Default;

jshygjg 发表于 2016-10-9 11:27:45

的快速减肥撒独立开发拉萨的看法发生了的空间

半个程序员 发表于 2017-1-4 22:43:49

好东西
页: [1] 2
查看完整版本: Delphi 关键字详解