1、打开Delphi7集成开发环境,在默认工程的Form1窗体上放置两个Button按钮,并分别设置其Caption属性为:显示记录和显示类

2、在默认工程的Unit1.pas文件的implementation中定义记录和类如下:implementation{$R *.dfm}type TTestRecord = record date:Tdate; end; TTestClass = class date:Tdate; end;

3、双击显示记录按钮进入按钮的事件方法,并写如下代码:procedure TForm1.Button1Click(Sender: TObject);var testRecord:TTestRecord;begin testRecord.date := Date; Showmessage(DatetoStr(testRecord.Date)+'长度为'+Inttostr(sizeof(testRecord)));end;在方法中定义了一个TTestRecord的变量,并给这个变量的date字段赋值,最后显示date和testRecord的长度。

4、F9运行程序,点击显示记录,弹出对话框,testRecord长度为8

5、双击显示类按钮进入按钮的事件方法,并写如下代码:procedure TForm1.Button2Click(Sender: TObject);var testClass:TTestClass;begin testClass := TTestClass.Create; testClass.date :=Date; Showmessage(DateToStr(testClass.date)+'长度为'+Inttostr(sizeof(testClass)));testClass.Free;end;定义了TTestClass的一个实例,并创建,赋值变量date字段后,显示长度后释放实例。

6、F9运行程序,点击显示记录,弹出对话框,testClass长度为4

7、总结一下记录类型在栈分配内存,类在堆分配,仅存一个指针。类的实例需要create和free,记录变量不需要.