随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

//间接嵌套
procedure TForm1.Button1Click(Sender: TObject);
type
  TWife = record  {妻子的数据结构}
    name: string;
    age: Word;
  end;

  TMan = record   {男人的数据结构}
    name: string;
    age: Word;
    wife: TWife;  {这里嵌套了结构}
  end;
var
  man: TMan;
begin
  man.name := '张三';
  man.age := 63;
  man.wife.name := '马翠';
  man.wife.age := 36;

  ShowMessage(man.name + #32 + man.wife.name); {张三 马翠}

  ShowMessage(IntToStr(SizeOf(man)));
end;


//直接嵌套
procedure TForm1.Button2Click(Sender: TObject);
type
  TMan = record   {男人的数据结构}
    name: string;
    age: Word;
    wife: record name: string; age: Word; end; {直接嵌套结构}
  end;
var
  man: TMan;
begin
  man.name := '张三';
  man.age := 63;
  man.wife.name := '马翠';
  man.wife.age := 36;

  ShowMessage(man.name + #32 + man.wife.name); {张三 马翠}
end;


//另外, 说到直接嵌套, 其实结构事先不定义, 也是可以直接使用的, 如:
procedure TForm1.Button3Click(Sender: TObject);
var
  man: record name: string; age: Word; end;
begin
  man.name := '李四';
  man.age := 81;

  ShowMessage(man.name); {李四}
end;
{
  注意没有, 在本页的例子中, 结构中的 name 被声明为 string !
  结构中不是不允许长字符串吗?
  其实 Delphi 在这里是把 string 当作 ShortString 用的.
  也就是说这里的 string 最大容量是 255 个字符, 127 个汉字.
}
end.

posted on 2008-01-09 18:01  万一  阅读(...)  评论(...编辑  收藏