比如在一个函数中,我想定义一个静态的变量,每次进入函数时都会自动加一,如果用一般的变量就实现不了,如果用全局变量又太小题大做了。不知是否有解决方法?还有在DELPHI中是否能定义一个静态的成员函数?
解决方案 »
- 你知道 SysShadow 嗎? 請賜教!
- 急急急!!!!!****还是保存十六进制文件的问题****
- 如何检测系统中是否安装摄像头
- 关于DBgrid的读写问题,高手快过来看看,高分乡镇
- 如何读写注册表中鍵的默认值?(在线)
- 高手们,帮帮忙!!!!
- Delphi有没有这种表格式的录入第三方控件
- 有誰教教我学电脑!
- 哪里有可用于输入IP地址的控件或算法
- Venne 请再看一下我的问题。
- 偶是牛虻(极有分的人),师父们不在,是胸闷怎么不理偶?问题:偶用三个范式规范数据表,但是输入数据时,需要把个个表的字段一起显示在一个dbgrid里,也就是要用的字段分布在不同表里,怎么版呢?
- 为什么delphi6生成的.exe文件拿到别的电脑就不能运行? 有什么可以解决的办法吗?
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,。
int GetCount(void)
{
static int nCount;
nCount++;
return nCount;
}
如果我需要也象C++这样在DELPHI的过程中申明一个类似的记数变量(不是在过程以外),可以实现吗?
我仅知道pascal肯定没有这种“静态局部变量";
const nCount:integer=0;搞定!!给分哈
FrmX:TForm=nil;