Delphi Helper Record Class

unit Unit1;

{$DEFINE USESGUIDHELP}

interface

implementation

{$IFDEF USESGUIDHELP}
uses System.SysUtils;
{$ENDIF}

procedure test;
var
  a:TGUID;//System單元
begin
  {$IFDEF USEGUIDHELP}
  a.NewGuid   //調用System.SysUtils.TGuidHelper
  a.Create()  //調用System.SysUtils.TGuidHelper
  {$ENDIF}
end;

end.

 

  PGUID = ^TGUID;
  TGUID = packed record
    D1: LongWord;
    D2: Word;
    D3: Word;
    D4: array[0..7] of Byte;
    class operator Equal(const Left, Right: TGUID): Boolean;
    class operator NotEqual(const Left, Right: TGUID): Boolean;
    class function Empty: TGUID; static;
  end;
type
  TGuidHelper = record helper for TGUID
    class function Create(const B: TBytes): TGUID; overload; static;
    class function Create(const S: string): TGUID; overload; static;
    class function Create(A: Integer; B: SmallInt; C: SmallInt; const D: TBytes): TGUID; overload; static;
    class function Create(A: Integer; B: SmallInt; C: SmallInt; D, E, F, G, H, I, J, K: Byte): TGUID; overload; static;
    class function Create(A: Cardinal; B: Word; C: Word; D, E, F, G, H, I, J, K: Byte): TGUID; overload; static;
    class function NewGuid: TGUID; static;
    function ToByteArray: TBytes;
    function ToString: string;
  end;

 

一、Record不支持繼承,使用Helper間接是完成了繼承ui

二、將Record分別在各自的單元(命名空間下),可選擇的使用Helper,便可在不影響原調用的狀況下,在擴展代碼中使用Helper的方法,也能夠無需修改原Record或class定義而直接覆蓋它們中的方法。spa

相關文章
相關標籤/搜索