delphi 代码助手_了解Delphi类(和记录)助手

delphi 代码助手

A feature of the Delphi language added some years ago (way back in in Delphi 2005) called "Class Helpers" is designed to let you add new functionality to an existing class (or a record) by introducing new methods to the class (record).

几年前( 在Delphi 2005中回溯)添加的Delphi语言功能“ Class Helpers”旨在通过向类(记录)引入新方法,使您向现有类(或记录)添加新功能。 。

Below you'll see some more ideas for class helpers + learn when to and when not to use class helpers.

在下面,您将看到有关班级帮手的更多想法,并了解何时以及何时不使用班级帮手。

班级帮手... ( Class Helper For... )

In simple words, a class helper is a construct that extends a class by introducing new methods in the helper class. A class helper allows you to extend existing class without actually modifying it or inheriting from it.

简而言之,类帮助器是通过在帮助器类中引入新方法来扩展类的构造。 类帮助器允许您扩展现有的类,而无需实际对其进行修改或从其继承。

To extend the VCL's TStrings class you would declare and implement a class helper like the following:

要扩展VCL的TStrings类,您将声明并实现一个类帮助器,如下所示:

type
TStringsHelper = class helper for TStrings
public
function Contains(const aString : string) : boolean;
end;

The above class, called "TStringsHelper" is a class helper for the TStrings type. Note that TStrings is defined in the Classes.pas, a unit that is by default available in the uses clause for any Delphi form's unit, for example.

上面的类称为“ TStringsHelper”是TStrings类型的类帮助器。 请注意,TStrings是在Classes.pas中定义的,Classes.pas是一个默认情况下可用的单位,例如,对于任何Delphi表单的单位,它的uses子句中都可用。

The function we're adding to the TStrings type using our class helper is "Contains". The implementation could look like:

我们使用类帮助器添加到TStrings类型的函数是“包含”。 该实现可能如下所示:

function TStringsHelper.Contains(const aString: string): boolean;
begin
result := -1 <> IndexOf(aString);
end;

I'm certain you've used the above many times in your code - to check if some TStrings descendant, like TStringList, has some string value in its Items collection.

我确定您已经在代码中多次使用以上代码-检查某些TString子代(例如TStringList)在其Items集合中是否具有某些字符串值。

Note that, for example, the Items property of a TComboBox or a TListBox is of the TStrings type.

请注意,例如, TComboBoxTListBox的Items属性为TStrings类型。

Having the TStringsHelper implemented, and a list box on a form (named "ListBox1"), you can now check if some string is a part of the list box Items property by using:

实现了TStringsHelper,并在窗体上创建了一个列表框(名为“ ListBox1”),您现在可以使用以下命令检查某些字符串是否是列表框Items属性的一部分:

if ListBox1.Items.Contains('some string') then ...

类帮助器Go and NoGo ( Class Helpers Go and NoGo )

The implementation of class helpers has some positive and some (you might think of) negative impacts to your coding.

类助手的实现对您的编码有一些积极的影响(可能会想到)。

In general you should avoid extending your own classes - as if you need to add some new functionality to your own custom classes - add the new stuff in the class implementation directly - not using a class helper.

通常,您应该避免扩展自己的类-就像您需要向自己的自定义类中添加一些新功能一样-直接在类实现中添加新内容-不使用类帮助器。

Class helpers are therefore more designed to extend a class when you cannot (or do not need to) rely on normal class inheritance and interface implementations.

因此,当您不能(或不需要)依赖普通的类继承和接口实现时,类帮助程序的设计目的是扩展类。

A class helper cannot declare instance data, like new private fields (or properties that would read/write such fields). Adding new class fields is allowed.

类助手不能声明实例数据,例如新的私有字段(或将读取/写入此类字段的属性)。 允许添加新的类字段。

A class helper can add new methods (function, procedure).

类帮助器可以添加新方法(函数,过程)。

Before Delphi XE3 you could only extend classes and records - complex types. From Delphi XE 3 release you can also extend simple types like integer or string or TDateTime, and have construct like:

在Delphi XE3之前,您只能扩展类和记录-复杂类型。 从Delphi XE 3发行版中,您还可以扩展简单类型,例如整数或字符串或TDateTime,并具有以下结构:

var
s : string;
begin
s := 'Delphi XE3 helpers';
s := s.UpperCase.Reverse;
end;

I'll write about Delphi XE 3 simple type helper in the near future.

我将在不久的将来写有关Delphi XE 3简单类型帮助器的文章。

我的班级助手在哪里 ( Where's MY Class Helper )

One limitation to using class helpers that might help you "shoot yourself in the foot" is the fact that 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子句中从右到左)。

What this means is that you might define two TStringsHelper class helpers in two different units but only one will apply when actually used!

这意味着您可以在两个不同的单元中定义两个TStringsHelper类帮助器,但实际使用时将仅应用一个!

If a class helper is not defined in the unit where you use its introduced methods - which in most cases will be so, you do not know what class helper implementation you would actually be using. Two class helpers for TStrings, named differently or residing in different units might have different implementation for the "Contains" method in the above example.

如果在使用其介绍方法的单元中未定义类帮助程序-在大多数情况下会如此,则您将不知道实际上将使用哪种类帮助程序实现。 TString的两个类帮助器(以不同的名称命名或位于不同的单元中)在上述示例中可能对“ Contains”方法具有不同的实现。

使用与否? ( Use Or Not? )

Yes, but be aware of the possible side effects.

是的,但是要注意可能的副作用。

Here's another handy extension to the above mentioned TStringsHelper class helper

这是上述TStringsHelper类帮助器的另一个便捷扩展

TStringsHelper =class helper for TStrings
private
function GetTheObject(const aString: string): TObject;
procedure SetTheObject(const aString: string; const Value: TObject);
public
property ObjectFor[const aString : string]: TObject read GetTheObject write SetTheObject;
end;
...
function TStringsHelper.GetTheObject(const aString: string): TObject;
var
idx : integer;
begin
result := nil;
idx := IndexOf(aString);
if idx > -1 then result := Objects[idx];
end;
procedure TStringsHelper.SetTheObject(const aString: string; const Value: TObject);
var
idx : integer;
begin
idx := IndexOf(aString);
if idx > -1 then Objects[idx] := Value;
end;

If you've been adding objects to a string list, you can guess when to use the above handy helper property.

如果您一直在将对象添加到字符串列表中 ,则可以猜测何时使用上述方便的helper属性。

翻译自: https://www.thoughtco.com/understanding-delphi-class-and-record-helpers-1058281

delphi 代码助手

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值