delphi - 我可以使用类助手调用静态私有(private)类方法吗?

标签 delphi

特别是,我觉得需要TCharacter.IsLatin1,它是private

type
  TCharacterHelper = class helper for TCharacter
  public
    class function IsLatin1(C: Char): Boolean; static; inline;
  end;

class function TCharacterHelper.IsLatin1(C: Char): Boolean;
begin
  Result := Ord(C) <= $FF;
end;

这种单行方法几乎可以立即重新实现,但我最好将具体的实现细节留给供应商自行决定。

有什么方法可以“重新引入”此方法到公共(public)可见性吗?

最佳答案

Is there any way to "reintroduce" this method to public visibility?

是的。通过新的类函数引入非静态函数调用。 这里的技巧是使用辅助功能通过 Self 访问所有成员。请参阅Access a strict protected property of a Delphi class?How do I use class helpers to access strict private members of a class? 。 这是通过从新类函数调用私有(private)辅助非静态函数来完成的,其中可以解析 Self

Type

  TCharacterHelper = class helper for TCharacter
  private
    class function IsLatin1Cracker(aChar: Char): Boolean; inline;
  public
    // Introduce a new public static class function
    class function IsLatinOne(aChar: Char): Boolean; static; inline;
  end;

class function TCharacterHelper.IsLatinOne(aChar: Char): Boolean;
begin
  Result := IsLatin1Cracker(aChar);
end;

class function TCharacterHelper.IsLatin1Cracker(aChar: Char): Boolean;
begin
  Result := Self.IsLatin1(aChar);  // Here Self can access base class
end;

虽然不能使用原来的方法名,但是仍然可以这样调用原来的类函数。

<小时/>

Oops, David showed a way to expand this idea to use the original name. That can be a versatile trick to have in the toolbox.

<小时/>

请参阅文档对此的说明:

Ordinary Class Methods :

You can use Self to call constructors and other class methods, or to access class properties and class fields.

Class Static Methods :

Unlike ordinary class methods, class static methods have no Self parameter at all.

注意:记录只能有静态类方法,与类不同。

Class and Record Helpers :

You can use the helper any place where you can legally use the extended class or record. The compiler's resolution scope then becomes the original type, plus the helper.

...

The visibility scope rules and memberList syntax are identical to that of ordinary class and record types.

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).

<小时/>

正如您上面所指出的,记录只能有静态类方法。因此,如果您想在记录中“重新引入”私有(private)类方法,这里有一个解决方案(基于 David 的技术):

假设我们有:

Type
  TTestRec = record
  private
    class Function IsLatin1(C: Char): Boolean; static; inline;
  end;

并在新单元中添加一个助手:

unit HelperUnitForTTestRec;

interface

Type
  TTestRecHelper = record helper for TTestRec
  public
    class function IsLatin1(c:Char): Boolean; static; //inline; !! Inlining not possible
  end;

implementation

Type
  TTestRecCracker = record helper for TTestRec
  private
    function IsLatinOne(C:Char): Boolean; inline;
  public
    class function IsLatin1Cracker(c:Char): Boolean; static; inline;
  end;

function TTestRecCracker.IsLatinOne(c: Char): Boolean;
begin
  Result := Self.IsLatin1(C);  // <-- Here is Self resolved
end;

class function TTestRecCracker.IsLatin1Cracker(c: Char): Boolean;
var
  tmp: TTestRec;
begin
  Result := tmp.IsLatinOne(C); // <-- Must use a call to ordinary method
end;

class function TTestRecHelper.IsLatin1(c: Char): Boolean;
begin
  Result := IsLatin1Cracker(C);
end;
    
end.

关于delphi - 我可以使用类助手调用静态私有(private)类方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21616948/

相关文章:

delphi - 如何在 Delphi WebService 中公开复杂类型

delphi - Delphi 中的 System.IsConsole 什么时候为真?

database - 在数据库表中快速定位

delphi - 如何在 Firemonkey 中创建径向渐变

mysql - Delphi中从SQL表接收数据

delphi - 什么可能导致编译器不产生控制台输出

windows - 在 CHOOSECOLOR 对话框中更改字体

delphi - DataSnap 是将现有 Windows 应用程序的数据和业务逻辑引入移动/Web 客户端的合适解决方案吗?

xml - Delphi - XML - 子节点 - 获取属性

delphi - 使用 Delphi 在网格面板中移动控件