如何使用类助手访问严格的私人成员类? [英] How do I use class helpers to access strict private members of a class?
问题描述
这是一个后续问题:如何隐藏对象的受保护的过程?
(我对整个类帮助器概念有点模糊)
假设我有一个类:
type
TShy = class(TObject)
strict private
procedure TopSecret;
private
procedure DirtyLaundry;
protected
procedure ResistantToChange;
end;
我知道我可以访问私有方法,如果我有源代码通过添加后代类在相同的单位。
I know I can access the private method if I have the source code by adding a descendent class in the same unit.
我有两个问题:
- 如何使用类帮助程序访问 strict private
member?
- 我可以使用单独的单元中的类帮助程序访问(严格)私人成员吗?
I have 2 questions:
- How do I employ a class helper to access the strict private
member?
- Can I use a class helper in a separate unit to access (strict) private members?
推荐答案
直到,包括Delphi 10.0 Seattle,您可以使用类帮助程序访问 strict protected
和 strict private
成员,如下所示:
Up to, and including Delphi 10.0 Seattle, you can use a class helper to access strict protected
and strict private
members, like this:
unit Shy;
interface
type
TShy = class(TObject)
strict private
procedure TopSecret;
private
procedure DirtyLaundry;
protected
procedure ResistantToChange;
end;
unit NotShy;
interface
uses
Shy;
type
TNotShy = class helper for TShy
public
procedure LetMeIn;
end;
implementation
procedure TNotShy.LetMeIn;
begin
Self.TopSecret;
Self.DirtyLaundry;
Self.ResistantToChange;
end;
end.
uses
..., Shy, NotShy;
procedure TestShy;
var
Shy: TShy;
begin
Shy := TShy.Create;
Shy.LetMeIn;
Shy.Free;
end;
类助手不能再访问 strict protected
, strict private
或 private
会员。这个功能实际上是Embarcadero现在在柏林修复的编译器错误。你没有运气。
However, starting with Delphi 10.1 Berlin, this no longer works! Class helpers can no longer access strict protected
, strict private
or private
members. This "feature" was actually a compiler bug that Embarcadero has now fixed in Berlin. You are out of luck.
这篇关于如何使用类助手访问严格的私人成员类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!