比如在一个函数中,我想定义一个静态的变量,每次进入函数时都会自动加一,如果用一般的变量就实现不了,如果用全局变量又太小题大做了。不知是否有解决方法?还有在DELPHI中是否能定义一个静态的成员函数?

解决方案 »

  1.   

    你可以看一下下面的例子程序:
    1.classone单元:定义了一个计数的Button控件
    2.mainform单元:动态产生控件,Caption表明它产生的序号unit classone;interface
    uses
      StdCtrls,Classes,Dialogs,SysUtils;type  tcountbutton = class(Tbutton)
        public
        constructor create(aowner:tcomponent);override;
        destructor destroy;override;
        procedure ReturnCaption(Sender: TObject);
        class function gettotal:integer;
      end;implementation
    var
      totbtns:integer=0;{ tcountbutton }constructor tcountbutton.create(aowner: tcomponent);
    begin
      inherited;
      inc(totbtns);
    end;destructor tcountbutton.destroy;
    begin
      dec(totbtns);
      inherited destroy;
    end;class function tcountbutton.gettotal: integer;
    begin
      result:=totbtns;
    end;procedure tcountbutton.ReturnCaption(Sender: TObject);
    begin
        showmessage('thsi is parent''s onclick event');
    end;initialization
        showmessage('total = ' + inttostr(tcountbutton.gettotal));
    finalization
        showmessage('total = ' + inttostr(tcountbutton.gettotal));
    end.
    unit mainform;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      ExtCtrls, StdCtrls;type
      Tmainfrm = class(TForm)
        Timer1: TTimer;
        procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure Timer1Timer(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure ReturnCaption(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      mainfrm: Tmainfrm;implementation
    uses
      classone;
    {$R *.DFM}procedure Tmainfrm.FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      with tcountbutton.create(self) do
        begin
          parent:=self;
          left := x;
          top := y;
          width := width+60;
          onclick := self.ReturnCaption;
          caption := format('the %dth button',[gettotal]);
        end;
    end;procedure Tmainfrm.Timer1Timer(Sender: TObject);
    begin
      caption:=format('Now there are %d buttons',[tcountbutton.gettotal]);
    end;procedure Tmainfrm.FormDestroy(Sender: TObject);
    begin
      messagebox(0,pchar(format('count = %d',[tcountbutton.gettotal])),'destroy',mb_ok);
    end;procedure Tmainfrm.ReturnCaption(Sender: TObject);
    begin
        showmessage((sender as tcountbutton).caption);
    end;
    end.注:Class method(类方法)中用的变量一般声明在Implemetation后面
    另外,Class method不能访问类中的fields, properties, and normal (object) methods,。
      

  2.   

    我还是不明白Class method是个什么东西。它同类成员变量有什么区别?以上的例子用类成员变量不也能解决吗?
      

  3.   

    Class Method是一种只对类起作用而不对对象起作用的方法。
      

  4.   

          很奇怪.不就是 Const 申明吗?说的这么玄?
      

  5.   

    你们还是没有解决我的问题。在C++中我们能这样申明一个函数:
    int GetCount(void)
    {
      static int nCount;
      nCount++;
      return nCount;
    }
    如果我需要也象C++这样在DELPHI的过程中申明一个类似的记数变量(不是在过程以外),可以实现吗?
      

  6.   

    以前也讨论过,delphi高手们说没有,只能用全局变量实现,
    我仅知道pascal肯定没有这种“静态局部变量";
      

  7.   

    在Implementation的前面用:
    const  nCount:integer=0;搞定!!给分哈
      

  8.   

    const
      FrmX:TForm=nil;