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.
  • 相关阅读:
    Fedora/CentOS使用技巧
    Haproxy配置
    iscsi使用教程
    Linux网络配置
    Linux命令使用
    luogu-1908 逆序对 离散化+树状数组
    算法题目签到表
    [笔记-机器学习]贝叶斯分类器的原理及实现
    [笔记-数据结构]树状数组
    [笔记-数据结构]哈希表
  • 原文地址:https://www.cnblogs.com/leonkin/p/2482593.html
Copyright © 2011-2022 走看看