随笔 - 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;
    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.

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