USEGEAR

导航

delphi类助手helpers,这个NB,省事、大幅提高效率

1、我们会想要能够对一个数据型别进行扩充,而不想继承类别。
2、如果我们想为一个组件类别加入新的方法,为它提供新的功能,而且不想通过继承来做。(如对TFDMEMTable增加方法等,你就得继承做处理,or做成控件进行安装...很繁琐)
那就使用class或者Record助手:
    这些特殊用途的数据型别能够延伸现有的型别,为这些型别加上新的方法。即使使用类别助手这个作法有些限制,类别助手让我们可以处理像刚刚提到的,只是要简单的为现有组件加入新方法的这种需求,而且不用修改现有的组件型

 

从XE版本开始提供类和记录的 Helper(助手) 封装,这样就可以很方便的在不修改原来类或者记录的原始定义情况下,给类和记录增加新的功能。 本文简要介绍 Helper 的实现,最后实际举例实现了TJSONObject的Helper,让使用delphi原生的JSON对象也能像SuperObject一样书写方式操作。(https://blog.csdn.net/sensor_WU/article/details/118051242)

  1.     Class Helpers 类助手是让我们为无法修改的类别(例如函式库类别)加入方法或属性的途径。使用类别助手来为我们自己写的类别做延伸并不常见,因为我们可以直接修改自己能掌握的类别的所有源码。
  2.     类别助手是 Delphi 的 Object Pascal 的特别创见,其他的编程语言则有像是扩充方法或者隐含类别来提供类似的功能。
  3.     类别助手无法加入实体数据,假设这个数据必须依赖实际的对象,而这些对象是以它们的原始类别定义的,或者碰触到原本定义在原始类别的实际架构中的虚拟方法,类别助手就帮不上忙了。

 

    换句话说,助手类别只能加入或者换掉已存在的非虚拟方法。我们可以用这个方法在原始类别的对象上加入新的方法,即使该类别当中没有现存方法的线索也一样。
Type
 TMyObject = class
 private
 Value: Integer;
 Text: string;
 public
 procedure Increase;
 end;
 TMyObjectHelper = class helper for TMyObject
 public
 procedure Show;
 end;
procedure TMyObjectHelper.Show;
begin
 Show (Text + ' ' + IntToStr (Value) + ' -- ' +
 ClassName + ' -- ' + ToString);
end;

1. Helpers语法定义:TJSONObjectHelper = class helper for TJSONObject

2.定义好Helper单元后,需要在使用的单元中引用,Helper(助手)才会起作用;

3.Helper是一种类的扩展方法,但不是设计时使用的方法,原始设计应该始终依赖于普通的类继承和接口实现。

感谢(https://blog.csdn.net/sensor_WU/article/details/118051242)

附: helpers(助手)实际应用

 

A、Delphi原生的JSON操作助手实现

 

使用Delphi的朋友都知道,三方JSON处理单元SuperObject.pas非常有名气,特别是操作JSON对象的方法非常简单。例如:jo.S[‘Name’] := ‘sensor’,或者 i := jo.I[‘age’],非常方便。但是delphi原生的操作起来,代码就多很多,例如:jo.TryGetValue(‘Name’,js),才能取出值,书写的代码比较多,繁琐,那么通过给TJSONObject 增加一个Helper(助手)就可以实现和SuperObject一样的操作方法。

 

S[]:表示字符串
I[]:表示整型数
I64[]:表示int63
D[]:表示日期
A[]:表示数组
O[]:表示TJSONObject对象
B[]:表示逻辑值

TJSONObject 的Helper 单元:
{**************************************
时间:2021-06-18
功能:1 实现delphi原生的JSON操作为 S[] 操作方式
作者:sensor
}
unit uSZHN_JSON;

interface
uses
  //System.Classes,
  //System.Types,
  //System.DateUtil,
  //System.Generics.Collections,
  //System.SysUtils,
  System.JSON;

type
  TJSONObjectHelper = class helper for TJSONObject
    private
       function  Get_ValueS(PairName : string) : string;
       procedure Set_ValueS(PairName,PairValue : string);

       function  Get_ValueI(PairName : string) : Integer;
       procedure Set_ValueI(PairName : string; PairValue : Integer);

       function  Get_ValueI64(PairName : string) : Int64;
       procedure Set_ValueI64(PairName : string; PairValue : Int64);

       function  Get_ValueD(PairName : string) : TDateTime;
       procedure Set_ValueD(PairName : string; PairValue : TDateTime);

       function  Get_ValueB(PairName : string) : Boolean;
       procedure Set_ValueB(PairName : string; PairValue : Boolean);

       function  Get_ValueA(PairName : string) : TJSONArray;
       procedure Set_ValueA(PairName : string; PairValue : TJSONArray);

       function  Get_ValueO(PairName : string) : TJSONObject;
       procedure Set_ValueO(PairName : string; PairValue : TJSONObject);

    public
      //判断某个字段是否存在
      function PairExists(PairName : string) : Boolean;

      //定义字段读取函数
      property S[PairName : string] : string      read Get_ValueS   write Set_ValueS;
      property I[PairName : string] : integer     read Get_ValueI   write Set_ValueI;
      property I64[PairName : string] : Int64     read Get_ValueI64 write Set_ValueI64;
      property D[PairName : string] : TDateTime   read Get_ValueD   write Set_ValueD;
      property B[PairName : string] : Boolean     read Get_ValueB   write Set_ValueB;
      property A[PairName : string] : TJSONArray  read Get_ValueA   write Set_ValueA;
      property O[PairName : string] : TJSONObject read Get_ValueO   write Set_ValueO;
  end;

implementation

{ TJSONObjectHelper }



function TJSONObjectHelper.Get_ValueS(PairName: string): string;
var
  js : TJSONString;
begin
  if PairName = '' then  Exit;

  if Self.TryGetValue(PairName,js) then
     Result := js.Value
  else
     Result := '';
end;

function TJSONObjectHelper.PairExists(PairName: string): Boolean;
begin
  Result := Self.Values[PairName] <> nil;
end;

procedure TJSONObjectHelper.Set_ValueS(PairName, PairValue: string);
var
  js : TJSONString;
begin
  //1. 首先查找有没有该字段, 如果有,则直接删除
  if Self.TryGetValue(PairName,js) then
     begin
       Self.RemovePair(PairName).Free; //如果没有free,就会产生内存泄露
     end;
  //2. 然后在增加
  Self.AddPair(PairName, PairValue);
end;

function TJSONObjectHelper.Get_ValueI(PairName: string): Integer;
var
  ji : TJSONNumber;
begin
  if PairName = '' then  Exit(0);

  if Self.TryGetValue(PairName,ji) then
     Result := ji.AsInt
  else
     Result := 0;
end;

procedure TJSONObjectHelper.Set_ValueI(PairName: string; PairValue: Integer);
var
  jn : TJSONNumber;
begin
  //1. 首先查找有没有该字段, 如果有,则直接删除
  if Self.TryGetValue(PairName,jn) then
     Self.RemovePair(PairName).Free;
  //2. 然后在增加
  Self.AddPair(PairName, TJSONNumber.Create(PairValue));
end;

function TJSONObjectHelper.Get_ValueD(PairName: string): TDateTime;
var
  ji : TJSONNumber;
begin
  if PairName = '' then  Exit(0);

  if Self.TryGetValue(PairName,ji) then
     Result := ji.AsDouble
  else
     Result := 0;
end;

procedure TJSONObjectHelper.Set_ValueD(PairName: string; PairValue: TDateTime);
var
  jn : TJSONNumber;
begin
  //1. 首先查找有没有该字段, 如果有,则直接删除
  if Self.TryGetValue(PairName,jn) then
     Self.RemovePair(PairName).Free;
  Self.AddPair(PairName, TJSONNumber.Create(PairValue));
end;


function TJSONObjectHelper.Get_ValueB(PairName: string): Boolean;
var
  jb : TJSONBool;
begin
  if PairName = '' then  Exit(False);

  if Self.TryGetValue(PairName,jb) then
     Result := jb.AsBoolean
  else
     Result := False;
end;

procedure TJSONObjectHelper.Set_ValueB(PairName: string; PairValue: Boolean);
var
  jb : TJSONBool;
begin
  //1. 首先查找有没有该字段, 如果有,则直接删除
  if Self.TryGetValue(PairName,jb) then
     Self.RemovePair(PairName).Free;
  Self.AddPair(PairName, TJSONBool.Create(PairValue));
end;


function TJSONObjectHelper.Get_ValueI64(PairName: string): Int64;
var
  ji : TJSONNumber;
begin
  if PairName = '' then  Exit(0);

  if Self.TryGetValue(PairName,ji) then
     Result := ji.AsInt64
  else
     Result := 0;
end;


procedure TJSONObjectHelper.Set_ValueI64(PairName: string; PairValue: Int64);
var
  jn : TJSONNumber;
begin
  //1. 首先查找有没有该字段, 如果有,则直接删除
  if Self.TryGetValue(PairName,jn) then
     Self.RemovePair(PairName).Free;
  Self.AddPair(PairName, TJSONNumber.Create(PairValue));
end;





function TJSONObjectHelper.Get_ValueA(PairName: string): TJSONArray;
var
  ja : TJSONArray;
begin
  if PairName = '' then  Exit(nil);

  Self.TryGetValue(PairName,Result);
end;





procedure TJSONObjectHelper.Set_ValueA(PairName: string; PairValue: TJSONArray);
var
  ja : TJSONArray;
begin
  //1. 首先查找有没有该字段, 如果有,则直接删除
  if Self.TryGetValue(PairName,ja) then
     Self.RemovePair(PairName).Free;

  Self.AddPair(PairName, PairValue);
end;


function TJSONObjectHelper.Get_ValueO(PairName: string): TJSONObject;
var
  jo : TJSONObject;
begin
  if PairName = '' then  Exit(nil);

  if Self.TryGetValue(PairName,jo) then
     Result := jo
  else
     Result := nil;
end;

procedure TJSONObjectHelper.Set_ValueO(PairName: string; PairValue: TJSONObject);
var
  jo : TJSONObject;
begin
  //1. 首先查找有没有该字段, 如果有,则直接删除
  if Self.TryGetValue(PairName,jo) then
     Self.RemovePair(PairName).Free;
  Self.AddPair(PairName, PairValue as TJSONObject);
end;

end.

 

 https://www.cnblogs.com/usegear/p/17091566.html

 

 

posted on 2023-02-04 15:20  USEGEAR  阅读(490)  评论(0编辑  收藏  举报