zoukankan      html  css  js  c++  java
  • 类字段 xe 单件模式 类方法

    TBall = class(TObject)
        class var
          shape: string;
          name: string;
          CommonBall: TBall;
        public
           class function NewInstance: TBall; //返回值  及 class function   //单件模式
            procedure FreeInstance; override;//释放类,使用该函数
           class function F2(k:Integer):integer;
        end;
    class function TBall.F2(k:Integer): integer;
    begin    
         shape:=IntToStr(k)
    end;

    procedure TBall.FreeInstance;
    begin
      inherited;
    end;
    class function TBall.NewInstance: TBall;
    begin
       if (not Assigned(CommonBall)) then begin
        CommonBall := TBall(inherited NewInstance);
      end;
      Result := CommonBall;
    end;

    var  
      CommonBall TBall= nil;    

    使用 
    var 
    Ball:TBall;
    begin
    Ball:=TBall.NewInstance;
    Ball,FreeInstance;//释放类,使用该函数
    end.
    类字段用于描述一个类的所有成员都拥有且值均相同的信息,例如我们可将国籍声明
    为一个类字段
    与单元中的 var 区域相似,class var 后也存在一个区域,声明于其中的所有字段均
    为类字段。此区域以 class var 开始,以下列 4 种情形的其中之一结束:
    1.  var 声明另一个 class var 声明
    2.  包括对象方法类方法、构造或析构函数在内的任何一个例程的声明
    3.  任意一个属性的声明
    4.  任何一个访问权限限定词(如 public、private 等)
    注意: 当 class var 区域出现了 const 声明的标识符时表示此标识符是一个类常量,并不
    表示 class var 区域的结束

    静态类方法
    静态类方法声明需在普通的类方法后加上限定词 static,如下所示:
    type
    T1 = class
    class function F2(var s:string):integer; static;
    end;
    静态类方法与普通的类方法非常相似,二者的唯一区别在于:类静态方法没有引含的
    self 参数,所以类静态方法不能引用任何的对象成员,但可以引用类字段和类方法。事实
    上,Delphi 引入这种类型的方法完全是为了与微软的.net 框架相互兼容。就功能上而言,
    普通的类方法完全可以替代静态类方法

    带参数的的单件
    type
      TCSingleton = class(TComponent) //从Tcomponent类继承来。
      private
        FAge: Integer;
         class var 类变量
          FCSingleton: TCSingleton;
          FRefCount: Integer;
        procedure  Class_Initialize;//初始化变量
        constructor CreateInstance(AOwner: TComponent); // 传递Owner参数
      public
        constructor Create(AOwner: TComponent); override;
        class function Instance(AOwner: TComponent): TCSingleton;

        procedure FreeInstance; override; //覆盖基类函数,释放类,使用该函数
        class function RefCount: Integer; //返回当前引用记数
        procedure ShowAge;
        procedure SetAgeValue(const i: Integer);
      end;

    implementation
    { TCSingleton }
    constructor TCSingleton.Create(AOwner: TComponent);
    begin
      //屏蔽Create函数的功能
      raise Exception.CreateFmt('Access class %s through Instance only', [ClassName]);
    end;

    constructor TCSingleton.CreateInstance(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      //可以在此初始化
      Class_Initialize;
    end;
    class function TCSingleton.Instance(AOwner: TComponent): TCSingleton;
    begin
      if (not Assigned(FCSingleton)) then begin
        //FCSingleton := TCSingleton(inherited NewInstance);
        FCSingleton := TCSingleton.CreateInstance(AOwner);
        //也可以在此初始化,见上面 constructor TCSingleton.CreateInstance(AOwner: TComponent);
        FCSingleton.Class_Initialize;
      end;
      Result := FCSingleton;
      Inc(FRefCount);
    end;

    procedure TCSingleton.Class_Initialize;
    begin
      FAge := 5;
    end;


    class function TCSingleton.RefCount: Integer;
    begin
      Result := FRefCount;
    end;

    procedure TCSingleton.SetAgeValue(const i: Integer);
    begin
      FAge := i;
    end;

    procedure TCSingleton.ShowAge;
    begin
      ShowMessage(IntToStr(FAge));
    end;

    procedure TCSingleton.FreeInstance;
    begin
      Dec(FRefCount); //减少引用记数
      if (FRefCount = 0) then begin //是否为0,是则释放内存
        FCSingleton := nil;
        // 释放单件类的私有变量
        inherited FreeInstance;
      end;
    end;


    XE 剪贴板类 单件模式
    type
      TClipboard = class(TPersistent)
      private
        //...
      public
        destructor Destroy; override;
      end;
    function Clipboard: TClipboard;//使用该方法,实现单件模式
    implementation
    var
      FClipboard: TClipboard;

    function Clipboard: TClipboard;
    begin
      if FClipboard = nil then
        FClipboard := TClipboard.Create;
      Result := FClipboard;
    end;

    destructor TClipboard.Destroy;
    begin
      if (FClipboard = Self) then
        FClipboard := nil;
      inherited Destroy;
    end;

    initialization
      FClipboard := nil;
    finalization
      if Assigned(FClipboard) then
        FClipboard.Free;
    end.

  • 相关阅读:
    操作系统基础知识
    os库基本介绍
    原型模式
    ASP .NetCore 部署500错误 查看异常详情
    css设置文本自动换行
    SqlServer数据库链接字符串
    Json列表数据查找更新
    VB中将类标记为可序列化
    VB 性能优化点
    参加公司工作总结会要准备的内容 IT 技术部
  • 原文地址:https://www.cnblogs.com/luckForever/p/7254642.html
Copyright © 2011-2022 走看看