网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 网络学院 >> 程序设计 >> Delphi编程 >> 文章正文
  Delphi 下全局对象的使用            【字体:
Delphi 下全局对象的使用
作者:佚名    文章来源:不详    点击数:    更新时间:2007-9-12    
本来在面向对象编程思想里是忌讳使用全局变量的。
正在装载数据……
如果非要使用,那么我们将需要用到的全局变量放到一个全局对象里,由全局对象对其进行管理。

再次申明:请谨慎使用全局变量!

具体做法是:
先新建一个单元文件:GlobalUnit.pas
在这个单元文件中申明全局类:TGlobalObject = class(TObject)
这个类里面可以存放你的全局变量,这个类的职责就只有维护你放到这个类里面的全局变量。
然后还要在这个单元文件中申明一个全局对象变量:var GlogalObject: TGlobalObject;
使用的时候,在所有要引用全局对象的单元文件前Uses这个GlobalUnit.pas单元文件。
然后在工程文件的开始创建全局对象,最后释放它。
注意:千万不要在除了工程文件以外的任何地方创建和释放它,否则后果自负哦!

下面给出一个例子。
这是全局单元:
unit GlobalUnit;

interface

uses
  SysUtils, Windows, Messages, Forms;

type
  TGlobalObject = class (TObject)
  private
    FMyGolbalVar1: Integer;     //这里是类的私有变量
    FMyGolbalVar2: string;
    procedure SetMyGolbalVar1(Value: Integer);
  public
    property MyGolbalVar1: Integer read FMyGolbalVar1 write SetMyGolbalVar1;
                 //这里提供变量的操作方法,也是外界访问全局变量的接口
    property MyGolbalVar2: string read FMyGolbalVar2 write FMyGolbalVar2;
  end;

var
  GlobalObject: TGlobalObject;   //申明全局对象变量

implementation

{
******************************** TGlobalObject *********************************
}
procedure TGlobalObject.SetMyGolbalVar1(Value: Integer);
begin       //这里对变量的写入进行了约束
  if Value <> FMyGolbalVar1 then
  begin
    if (Value >= 18) and (Value <= 60) then
      FMyGolbalVar1 := Value
    else
      raise ERangeError.CreateFmt('您输入的数值:%d 已经超出了我们要求的范围 %d..%d',
            [Value, 18, 60]);
  end;
end;


end.

下面是工程文件:
program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2},
  GlobalUnit in 'GlobalUnit.PAS';

{$R *.res}

begin
  Application.Initialize;
  GlobalObject := TGlobalObject.Create;  //创建全局对象
  Application.CreateForm(TForm1, Form1);
  Application.Run;
  GlobalObject.Free;                     //销毁全局对象
end.

下面是两个窗体单元:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    UpDown1: TUpDown;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure UpDown1ChangingEx(Sender: TObject; var AllowChange: Boolean;
      NewValue: Smallint; Direction: TUpDownDirection);
    procedure Edit2Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    FForm2: TForm2;   
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  FForm2.ShowModal;
  UpDown1.Position := GlobalObject.MyGolbalVar1;
  Edit2.Text := GlobalObject.MyGolbalVar2;
end;

procedure TForm1.UpDown1ChangingEx(Sender: TObject;
  var AllowChange: Boolean; NewValue: Smallint;
  Direction: TUpDownDirection);
begin
  try
    GlobalObject.MyGolbalVar1 := NewValue;
  except
    AllowChange := False;
    raise;      //继续抛出错误
  end;
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin
  GlobalObject.MyGolbalVar2 := Edit2.Text;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FForm2 := TForm2.Create(Self);
  GlobalObject.MyGolbalVar2 := Edit2.Text;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FForm2.Free;
end;

end.

unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

{var
  Form2: TForm2;
}
implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
  GlobalObject.MyGolbalVar1 := 35;
end;

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  GlobalObject.MyGolbalVar2 := Edit1.Text;
end;

procedure TForm2.FormShow(Sender: TObject);
begin
  Caption := GlobalObject.MyGolbalVar2;
end;

end.



本文来源:http://blog.csdn.net/chenyayu/archive/2007/08/29/1764536.aspx
站内文章搜索 高级搜索
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
     在delphi中使用xml文档有…
     初探delphi 7 中的插件编…
     delphi 2006(dexter) & …
     获得windows的版本信息。
     “序列号输入助手”源代…
     rs232串口通讯模块
     ado方式下判断数据表是否…
  • Ant入门-配置和使用     选…

  • 浅析Spring框架下PropertyPl…

  • SPRING+STRUTS+HIBERNATE登录…

  • MVP——Model-Viewer-Presen…

  • C++ Object Model

  • constructor and destructor

  • 绑定HGE到AngelScript脚本系…

  • delegate C#关键字 (委托类型…

  • Boyer-Moore String Searchi…

  • 【游戏制作基础】网络游戏设…

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    网络学院©2007 www.23book.net
    为您提供web编程,vb编程,vc编程,服务器架设管理,数据库设计等方面的知识 站长:David