随笔 - 2146  文章 - 97 评论 - 11761 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}

Type
  TMyRec = record     {定义结构}
    name: string[8];
    age : Word;
  end;
  PMyRec = ^TMyRec;   {定义结构指针; Delphi 一般把它定义在结构前面, 都可以}


{如果直接使用结构, 不使用指针}
procedure TForm1.Button1Click(Sender: TObject);
var
  r: TMyRec;
begin
  r.name := '张三';
  r.age := 99;
  ShowMessage(Format('%s %d 岁', [r.name, r.age])); {张三 99 岁}
end;

{如果使用结构指针}
procedure TForm1.Button2Click(Sender: TObject);
var
  pr: PMyRec;
begin
  New(pr);            {分配内存}
  pr^.name := '张三';
  pr^.age := 99;
  ShowMessage(Format('%s %d 岁', [pr^.name, pr^.age])); {张三 99 岁}
  Dispose(pr);        {释放内存}
end;

{Delphi 允许使用结构时做如下简化}
procedure TForm1.Button3Click(Sender: TObject);
var
  pr: PMyRec;
begin
  New(pr);
  pr.name := '张三';
  pr.age := 99;
  ShowMessage(Format('%s %d 岁', [pr.name, pr.age])); {张三 99 岁}
  Dispose(pr);
end;

end.

System 单元下的公用函数目录

posted on 2008-03-27 17:41 万一 阅读(...) 评论(...) 编辑 收藏