通過實驗,答案是肯定的!這使我感到很興奮,因為Delphi從誕生之日起,就限定了只能繼承於TObject的堆對象,必須通過Create和Free來建立和銷毀一個對象,而不能使用棧對象。而棧對象的好處就在於它能和其它類型一樣很方便的定義,它能自動調用建構函式和解構函式,在範圍範圍內(如函數內的局部對象)不必當心對象的建立和銷毀問題。要是Delphi具有了該功能,無意是Delphi編程者們的福音。
不過,進一步的測試卻比較失望,請看下面的代碼:
type
TMyRecord = record
private
x: Integer;
y: Integer;
public
// error: Parameterless constructors not allowed record types
// constructor Create;
// error: A record cannot introduce a destructor
// destructor TMyRecird;
constructor Create(a: Integer);
constructor TMyRecord; overload;
constructor TMyRecord(a, b: Integer); overload;
// constructor TMyRecord(a, b: Integer);
procedure Display;
property xi: Integer read x;
end;
{ TMyRecord }
constructor TMyRecord.Create(a: Integer);
begin
x := a;
y := 0;
end;
procedure TMyRecord.Display;
begin
ShowMessage(Format('x = %d, y = %d', [x, y]));
end;
constructor TMyRecord.TMyRecord;
begin
x := 0;
y := 0;
end;
constructor TMyRecord.TMyRecord(a, b: Integer);
begin
x := a;
y := b;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
rec: TMyRecord;
begin
rec.TMyRecord(100, 200);
rec.Display;
end;