天天看點

delphi2006語言新特性:Record類型進階用法

delphi語言在傳統的Records類型的基礎上增加了許多像類一樣的進階功能,如:Records可以有屬性和方法(包括構造constructors),類屬性,類方法,類靜态字段和内嵌類型。下面這個示例示範定義一個功能像類一樣的Records:

type

TMyRecord = record

TInnerColorType = Integer;

var

Red: Integer;

class var

Blue: Integer;

procedure printRed();

constructor Create(val: Integer);

property RedProperty: TInnerColorType read Red write Red;

class property BlueProp: TInnerColorType read Blue write Blue;

end;

constructor TMyRecord.Create(val: Integer);

begin

Red := val;

procedure TMyRecord.printRed;

writeln('Red: ', Red);

雖然現在records能實作許多類的特性,但它與類之間還是有一些不同:

1 records不支援繼承

2 records能含有variant parts(呵呵,不知翻譯成什麼能說明白,就是case部分),類不可以

3 records是值類型,可以通過指派拷貝,類是參考類型,是以不能通過指派來拷貝。

4 records在win32和.net上允許操作符重載,類僅在.net上支援操作符重載

5 records使用一個預設的沒有參數的構造函數(constructor)自動建立,而類必須明确的建立。在record中使用者定義的構造函數必須有一個或多個參數。

6 record類型不能有析構函數(destructors)

7 虛方法(那些指定了virtual、dynamic和message關鍵字的)不能使用在record類型中

8 record類型在win32開台上不能實作接口,在.net平台上能實作接口

繼續閱讀