现在的位置: 首页 > 综合 > 正文

Delphi中的Type

2014年01月09日 ⁄ 综合 ⁄ 共 2739字 ⁄ 字号 评论关闭

声明:

1. type Name = Existing type;
2. type Name = type Existing type;
3. type Name = (EnumValue1 [=value], EnumValue2 [=value] ...);
4. type Name = Expression1..Expression2;
5. type Name = ^Existing type;
6. type Name = array[...] of Existing type;
7. type Name = class ... end;
8. type Name = class of Existing class
9. type Name = dispinterface ... end;
10. type Name = file of Existing type;
11. type Name = function ...;
12. type Name = interface ... end;
13. type Name = object ... end;
14. type Name = procedure ...;
15. type Name = record ... end;
16. type Name = set of Ordinal values

描述:Type关键词是Delphi的基础部分,不像其它语言,它允许创建变量和过程的新类型(种类)。这些新的类型可以被引用,就像它是语言的一部分一样。

比如:Type TCourtCards = (Ace,Jack,Queen,King);

允许定义一个这种“type”的新变量:var Card:TCourtCard; Card:=Queen;

一般都在类型名字的前缀加“T”,这是一种有用的约定。

1.type Name = Existing type

参考一个现有的类型,比如用新的名字来替代string。

2.type Name = type Existing type

这个与上面的效果一样,但是它确保在运行时,这个类型的变量被识别为它的新类型名称,而不是原先已存在的类型名称。

3.type Name = (EnumValue1,EnumValue2…)

定义一个枚举类型,包含了值EnumValue1,EnumValue2等等。它是用户自定义的名字,列举出所有有可能的值。这些值在你的程序中必 须是唯一的,一旦在类型中定义了它,只可通过两种方法引用:指定或引用一个这种类型的变量;和用Ord关键词取得序数作为数字值。见例子。

注:这些枚举元素在定义的时候,它的位置值为0,1,2,等等,除非这个值用“=value”覆写。如:

Type Days = (Monday = 1,Tuesday,Wed…);

这里,Monday位置值被设为1,Tuesday为2,Wednesday 为3,依次类推。

4.Type Name = Expression1..Expression2

这里是一个完整的整数或字符范围,从Expression1表达式到Expression2表达式。表达式1和2可以是计算结结果为整数或字符的公式,或仅是整数或字符常量。如:Type TAlphabet =’A’..’z';通常用于定义字符范围,从大写A到小写z。

5.type Name = ^Existing type

‘^’符号是指向现有类型的指针。它经常用于导航记录Record类型。

6.type Name = array[…] of existing type

一个结构类型,用一个新的类型封装了某类型的数组。

7.type Name = class…end

用于定义一个新类的结构,详见Class关键词。

8.type Name = class of existing class

提供一个meta-class定义,详见Class关键词。

9.type Name = dispinterface … end

一个分派接口(dispatch interface)类型,详见Dispinterface关键词。

10.type Name = file of Existing type

定义一个指向文件的类型,这个文件包含了给定类型的记录。(默认地,文件包含二进制数据)

11.type Name = function …

定义一个函数当作类型,允许这个函数被定义作为参数用于子程序。

12.type Name = interface … end

用于定义接口的结构。详见Interface关键词。

13.type Name = object … end

相当于类定义,已过时淘汰。

14.type Name = procedure …

定义一个过程当作类型,允许这个过程被定义作为参数用于子程序。

15.type Name = record … end

定义记录类型,在给定的名称下封装数据结构。详见Record关键词。

16.type Name = set of Ordinal values

定义有序数的子界。定义了一个整数或字符的范围。详见Set关键词。

{举一些type例子}
Type
TString1 = string; // 1. type Name = Existing type
TString2 = type string; // 2. type Name = type Existing type
TTemp = (Hot, Warm, Cold); // 3. type Name = (Enum1, Enum2 ...)
TExpr = 5*2 .. 6*3; // 4. type Name = Expr1 .. Expr2
// 5. See the Pointer keyword
TArray = array[1..3] of byte; //6. type Name = array[...] of type
// 7. 见TFrom1类定义
// 8. 详见 Class 关键词
// 9. 详见 Dispinterface 关键词
// 10. 详见 File 关键词
// 11. 详见 Function 关键词
// 12. 详见 Interface 关键词
// 13. 淘汰不用了
// 14. 详见 Procedure 关键词
TRecord = record // 15. type Name = record .. end;
header : string;
value : Integer;
end;
TLetters = set of 'A'..'z'; // 16. type Name = set of Ordinals

var
// 用上面的类型定义变量
firstName : TString1;
lastName : TString2;
temperature : TTemp;
expression : TExpr;
myArray : TArray;
myRecord : TRecord;
letters : TLetters;

begin
// 为变量赋值
firstName := 'Neil';
lastName := 'Moffatt';
temperature := Cold;
expression := 10;
myArray[1] := 5;
myRecord.header := 'data file';
letters := ['F'..'Q'];
end; 

抱歉!评论已关闭.