如何使用类助手访问类的严格私有成员? [英] 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
成员?
- 我可以在单独单元中使用类助手来访问(严格的)私有成员吗?
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;
但是,从 Delphi 10.1 Berlin 开始,这不再有效!类助手不能再访问 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屋!