您的位置: 首页-> 电脑文摘-> 程序设计-> Delphi/Pascal-> 正文

Delphi中静态属性及静态方法实现
作者佚名 来源InterNet 加入时间:2004-11-25
在学习Delphi时常有些心得,在此写出来,供大家参考,如有错误或不妥之处还望指教.

    使用过c++的人都知道在c++的类中有静态属性及静态方法,为程序设计带来很多方便.那么在Delphi中静态属性及静态方法是怎么实现的呢?请看下面的实例:

unit Unit2;

interface

type
  TMyClass = Class
  public
    {静态过程:设置静态属性的值}
    class procedure SetStaticMemberValue(AString: string);
    {静态函数:读取静态属性的值}
    class function GetStaticMemberValue: string;
  end;

implementation
{在此声明静态属性,这一点与c++有很大的不同}
var
  AStaticMember: string;

class function TMyClass.GetStaticMemberValue: string;
begin
  Result := AStaticMember;
end;

class procedure TMyClass.SetStaticMemberValue(AString: string);
begin
  AStaticMember := AString;
end;

end.

    那么在TMyClass中声明的属性及方法是否是静态属性或静态方法呢?请看下面的实例:

...
uses unit2
...

procedure TForm1.Button2Click(Sender: TObject);
begin
  {不需声明TMyClass的实例,可直接设置及读取静态属性的值}
  TMyClass.SetStaticMemberValue('MyClass');
  showmessage(TMyClass.GetStaticMemberValue);
end;




[文章录入员:fightter]

相关文章 相关软件:
::PCBOOKCN'ADS::


::Delphi/Pascal::
C/C++/VC
C++Builder
Basic/VB类
Delphi/Pascal
Java编程
FORTRAN
其它
::阅读排行::
·Delphi与Excel的亲密接...
·Delphi实现串口通信的常用的...
·DELPHI下的多线程程序设计(...
·Delphi中初始化.Ini文件...
·Delphi中用于读写(I/O)...
·谈谈Delphi中的类和对象
·Delphi编译错误中文手册
·Delphi中关于TApplic...
·OPENGL图形程序设计
·SQL的基本操作