Delphi实现静态变量

Delphi实现静态变量



C++有静态变量,static关键字描述,其实Delphi也可以做到。


以前一般采取的是const办法来实现,如今的Delphi可以用class关键字来实现。


附代码如下,两种体式格式具有示例。



 1 unit Unit6;

 2 

 3 interface

 4 

 5 uses

 6   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,

 7   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

 8 

 9 type

10   TTestClass = class

11   private

12     {$J+}

13     const FTest : integer = 0;

14     {$J-}

15     class var FTest2 : integer;

16     function GetTest: integer;

17     function GetTest2: integer;

18   public

19     constructor Create;virtual;

20 

21     property Test : integer read GetTest;

22     property Test2 : integer read GetTest2;

23   end;

24 

25   TForm6 = class(TForm)

26     Button1: TButton;

27     procedure Button1Click(Sender: TObject);

28   private

29     { Private declarations }

30   public

31     { Public declarations }

32   end;

33 

34 var

35   Form6: TForm6;

36 

37 implementation

38 

39 {$R *.dfm}

40 

41 { TTestClass }

42 

43 constructor TTestClass.Create;

44 begin

45   Inc(FTest);

46   Inc(FTest2);

47 end;

48 

49 function TTestClass.GetTest: integer;

50 begin

51   Result := FTest;

52 end;

53 

54 function TTestClass.GetTest2: integer;

55 begin

56   Result := FTest2;

57 end;

58 

59 procedure TForm6.Button1Click(Sender: TObject);

60 var

61   ATest : TTestClass;

62 begin

63   ATest := TTestClass.Create;

64   Caption := IntToStr(ATest.GetTest)+"":""+IntToStr(ATest.GetTest2);

65 end;

66 

67 end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值