zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    修复 Visual Studio Error “No exports were found that match the constraint”
    RabbitMQ Config
    Entity Framework Extended Library
    Navisworks API 简单二次开发 (自定义工具条)
    NavisWorks Api 简单使用与Gantt
    SQL SERVER 竖表变成横表
    SQL SERVER 多数据导入
    Devexpress GridControl.Export
    mongo DB for C#
    Devexress XPO xpPageSelector 使用
  • 原文地址:https://www.cnblogs.com/codingnote/p/2478692.html
Copyright © 2011-2022 走看看