首页 > 其他分享 >delphi类助手helpers,这个NB,省事、大幅提高效率

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

时间:2023-02-04 15:37:42浏览次数:36  
标签:Set end string Get Self NB PairName helpers delphi

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

 

 

标签:Set,end,string,Get,Self,NB,PairName,helpers,delphi
From: https://www.cnblogs.com/usegear/p/17091566.html

相关文章

  • vue计算属性的完整写法-cnblog
    简单写法,给计算属性赋值报错解决方案:提供set方法实例(全选-->反选,反选->全选)<template><div><span>全选:</span><!--4.v-model关联全选-选中状态......
  • vue作用域插槽-cnblog
    使用场景:父组件需要使用到子组件的data时例子父组件有list数组,传递给子组件,子组件将数组中的属性全部显示出来,但是用什么标签显示这些属性不确定(使用插槽)父组件传......
  • vue侦听器深度侦听-cnblog
    简单类型不需要加上引号如对象user.name(注意加上引号)watch:{ "obj.name":{ ...}}侦听器的应用(存入本地存储localStorage)也可以在组件销毁时存入本......
  • async和await-cnblog
    在之前对promise对象的处理使用Promise原型函数then,catch解决回调地狱的问题,但存在冗余代码.then().then().then()asyncawaites8(esma2017)引入awaitPromis......
  • QT中级(1)QTableView自定义委托(一)实现QSpinBox、QDoubleSpinBox委托
    1写在前面的话我们在之前写的《QT(7)-初识委托》文章末尾提到,“使用一个类继承QStyledItemDelegate实现常用的控件委托,在使用时可以直接调用接口,灵活实现各种委托”。我......
  • 真是太棒了,可以在win11上使用unbuntu的系统命令
    我其实是好久没有用过win的系统了,这次用还是听惊喜的,可以看到下面的这个图。如何安装一、升级wsl​​https://learn.microsoft.com/en-us/windows/wsl/install-manual#step-......
  • JavaScript高级第01天笔记-cnblog
    JavaScript高级第01天笔记1.面向过程与面向对象1.1面向过程面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候再一个一个的依次调用......
  • JavaScript高级第02天笔记-cnblog
    JavaScript高级第02天笔记1.构造函数和原型1.1对象的三种创建方式--复习字面量方式varobj={};new关键字varobj=newObject();构造函数方式function......
  • JavaScript高级第03天笔记-cnblog
    JavaScript高级第03天笔记1.函数的定义和调用1.1函数的定义方式方式1函数声明方式function关键字(命名函数)functionfn(){}方式2函数表达式(匿名函数)var......
  • Echarts使用第一天-cnblog
    Echarts使用第一天1.使用示例<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge">......