Delphi Helper Record Class
阅读原文时间:2023年07月10日阅读:1

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[..] 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;

1、Record不支持继承,使用Helper间接是完成了继承

2、将Record分别在各自的单元(命名空间下),可选择的使用Helper,即可在不影响原调用的情况下,在扩展代码中使用Helper的方法,也可以无需修改原Record或class定义而直接覆盖它们中的方法。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章