Delphi静态变量

在Delphi中也可以定义静态变量, 有两种方法:

一是使用编译开关{$J+}{$J-}包含一个常量定义

二是使用指针操作常量

下面列出示例代码:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure MyFunc1();
{$J+}
const iTmp: Integer = 100;
{$J-}
begin
  ShowMessage(IntToStr(iTmp));
  Inc(iTmp);
end;

procedure MyFunc2();
const
  iTmp: Integer = 200;
begin
  ShowMessage(IntToStr(iTmp));
  Inc(Integer(Pointer(Integer(@iTmp))^));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  MyFunc1();
  MyFunc2();
end;

end.


展开阅读全文

没有更多推荐了,返回首页