简体   繁体   English

字符串类型可以使用两个记录助手吗?

[英]Is it possible to use two record helpers for the string type?

I created this helper in order to add some more functions to the string type: 我创建此帮助程序是为了向string类型添加更多功能:

type
  AStringHelper = record helper for string
    function Invert: string; overload;
    function InvertMe: string; overload;
  end;

But when I use it in my code, the TStringHelper in System.StrUtils "gets out" and I can't use it's functions. 但是,当我在代码中使用它时, System.StrUtilsTStringHelper会“退出”并且无法使用它的功能。

Is it possible to both of them to coexist? 他们是否可以共存?

At most one helper can active at any one point in your code. 您的代码中的任何一点最多只能有一个助手。 The documentation says this: 文件说:

You can define and associate multiple helpers with a single type. 您可以使用单个类型定义和关联多个助手。 However, only zero or one helper applies in any specific location in source code. 但是,在源代码中的任何特定位置仅适用零个或一个辅助函数。 The helper defined in the nearest scope will apply. 在最接近的范围内定义的帮助程序将适用。 Class or record helper scope is determined in the normal Delphi fashion (for example, right to left in the unit's uses clause). 类或记录帮助程序的作用域以常规的Delphi方式确定(例如,在该单元的uses子句中从右到左)。

Since record helpers do not support inheritance, there's no way to have both the standard helper's and your helper's functionality active at the same time, other than re-implementing the standard helper's functionality. 由于记录助手不支持继承,因此除了重新实现标准助手的功能外,无法同时激活标准助手和您的助手功能。

Try to create your own class and helper 尝试创建自己的班级和助手

TMyString = type string;
TMyStringHelper = record helper for TMyString 
  function Invert: TMyString; 
  function ToString: string;
end;

Use TMyString instead of String. 使用TMyString而不是String。

To use standard String helpers wrap TMyString variable with String() or use ToString method. 要使用标准的字符串帮助器,请在TMyString变量中加上String()或使用ToString方法。

var
  S1: TMyString;
  S2: String;
begin
  S1 := ' 123456 ';
  S1.Invert;
  S2 := String(S1).Trim;
  S2 := S1.ToString.Trim;
end;

A simple solution to this could be: 一个简单的解决方案可能是:

type
  TStr = record
  private
    FStr:string;
  public
    function Invert: string; overload;
    function InvertMe: string; overload;
  end;

By using type casting you can access the functions on TStr on a ordinary string variable: 通过使用类型转换,您可以在普通string变量上访问TStr上的函数:

var
  s1,s2:string;
begin
  s1:='123456';
  s2:=TStr(s1).invert;
end;

But of course, then we can ask: Why not just write an ordinary function? 但是,当然,我们可以问:为什么不编写普通的函数呢? :-) :-)

Anyway, I like this class / object / record method syntax better than traditional function/procedure. 无论如何,我比传统的函数/过程更喜欢这种class / object / record方法的语法。

Another possibility is to use the "Ancestor list" option offered by helpers 另一种可能性是使用助手提供的“祖先列表”选项

Syntaxis: 句法:

type
identifierName = class|record helper [(ancestor list)] for TypeIdentifierName
  memberList
end;

And use like this: 像这样使用:

Unit1:   
THelperBase = Class helper for TMyClass
Public
  Function SayHello;
end;

Unit2:
Uses Unit1;
THelperChild = Class helper (THelperBase) for TMyClass
Public
  Function SayGoodBye;
end;

And finally, in your code: 最后,在您的代码中:

Uses Unit2;   

Var MyClass: TMyclass;   

MyClass:= TMyclass.Create;
MyClass.SayHello; //valid
MyClass.SayGoodBye; //valid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM