類別變數/函式的使用

1 篇文章 / 0 new
author
類別變數/函式的使用
program StaticTest;
{$APPTYPE CONSOLE}
 
uses SysUtils, Classes;
type
  TTestClass = class
  private
    class var FStaticField: TStrings;
    class function GetStaticField: string; static;
    class procedure SetStaticField(const Value: string); static;
  public
    class property StaticField: string read GetStaticField write SetStaticField;
    class constructor CreateClass;
    class destructor DestroyClass;
  end;
 
{ TTestClass }
 
class constructor TTestClass.CreateClass;
begin
  FStaticField := TStringList.Create;
  FStaticField.Text := 'Hello world!';
end;
 
class destructor TTestClass.DestroyClass;
begin
  FStaticField.Free;
end;
 
class function TTestClass.GetStaticField: string;
begin
  Result := FStaticField.Text;
end;
 
class procedure TTestClass.SetStaticField(const Value: string);
begin
  FStaticField.Text := Value;
end;
 
begin
  WriteLn(TTestClass.StaticField);
  ReadLn; // Stop window from closing
end.
from http://ibeblog.com/
Free Web Hosting