这是一个后续问题:How to hide a protected procedure of an object?
(我对整个班级助手的概念有点模糊)
假设我有一个类:
type
TShy = class(TObject)
strict private
procedure TopSecret;
private
procedure DirtyLaundry;
protected
procedure ResistantToChange;
end;
我知道如果我通过在同一单元中添加一个后代类来获得源代码,我可以访问私有方法。
我有两个问题:
- 如何使用班助手访问strict private
成员?
- 我可以在单独的单元中使用类帮助程序来访问(严格)私有成员吗?
答案 0 :(得分:37)
直到并且包括Delphi 10.0 Seattle,您可以使用类帮助程序来访问strict protected
和strict private
成员,如下所示:
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柏林开始,这不再有效!类帮助者无法再访问strict protected
,strict private
或private
成员。这个“功能”实际上是Embarcadero现已在柏林修复的编译器错误。你运气不好。
答案 1 :(得分:7)
在Delphi 10.1 Berlin中删除了使用private
访问班级的strict private
和class helpers
成员。请参阅Closing the Class Helpers Private Access Loophole。
但仍存在一个漏洞:
unit Shy;
interface
type
TShy = class(TObject)
strict private
procedure TopSecret;
private
procedure DirtyLaundry;
protected
procedure ResistantToChange;
end;
implementation
procedure TShy.DirtyLaundry;
begin
WriteLn('DirtyLaundry');
end;
procedure TShy.ResistantToChange;
begin
WriteLn('ResistantToChange');
end;
procedure TShy.TopSecret;
begin
WriteLn('TopSecret');
end;
end.
Program TestClassHelpers;
{$APPTYPE CONSOLE}
Uses
Shy;
type
TNotShy = class helper for TShy
public
procedure LetMeIn;
end;
procedure TNotShy.LetMeIn;
var
P : procedure of object;
begin
TMethod(P).Code := @TShy.TopSecret;
TMethod(P).Data := Self;
P; // Call TopSecret
TMethod(P).Code := @TShy.DirtyLaundry;
TMethod(P).Data := Self;
P; // Call DirtyLaundry;
Self.ResistantToChange; // Protected access works without problems
end;
var
myObj: TShy;
begin
myObj := TShy.Create;
try
myObj.LetMeIn;
ReadLn;
finally
myObj.Free;
end;
end.