Delphi中学习结构[记录]类型用法

unit Unit1;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.dfm}

//如果要记录一个人的姓名和年龄, 可以:
procedure TForm1.Button1Click(Sender: TObject);
var
name: string;
age: Word;
begin
{赋值}
name := '张三';
age := 18;

{读取}
ShowMessage(Format('%s今年%d岁', [name,age])); {张三今年18岁}
end;


//不如定义一个结构类型
procedure TForm1.Button2Click(Sender: TObject);
type
TPerson = record
    name: string[12]; {在结构里面不能使用长字符串}
    age: Word;
end;
var
person: TPerson;        {声明结构变量}
const
str = '%s今年%d岁';     {为格式化输出准备一个常量}
begin
{赋值}
person.name := '李四';
person.age := 81;

{读取}
ShowMessage(Format(str, [person.name,person.age])); {李四今年81岁}
end;

end.

你可能感兴趣的:(Delphi)