您的位置:首页 > 其它

学习结构[记录]类型(7) - 结构也可以有构造函数

2008-01-09 16:59 288 查看
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

type
TRec = record    {定义结构 TRec}
name: ShortString;
age: Word;
constructor Create(str: ShortString; w: Word); {构造函数}
end;

{ TRec 构造函数实现}
constructor TRec.Create(str: ShortString; w: Word);
begin
name := str;
age := w;
end;

//使用结构
procedure TForm1.Button1Click(Sender: TObject);
var
rec: TRec;
begin
rec.Create('李四', 81);
ShowMessage(rec.name); {李四}
end;

end.

结构的方法、属性都是在 Delphi 7 以后的版本中加入的, 非常类似与"类", 但又不如在"类"里完善;

它的其他一些新特性也好像是从"类"里搬过来的, 等在"类"里面研究吧.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: