码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi Helper Record Class

时间:2015-04-02 14:56:07      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

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;

 

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

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

 

Delphi Helper Record Class

标签:

原文地址:http://www.cnblogs.com/ZhouXiHong/p/4386715.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!