【发布时间】:2015-08-12 17:55:37
【问题描述】:
给定
type
TMyClass = class
private
FPrivateInt : Integer;
protected
FProtectedInt : Integer;
public
FPublicInt : Integer;
end;
在一个单元中
type
TMyHelper = class helper for TMyClass
function Sum : Integer;
end;
[...]
function TMyHelper.Sum: Integer;
begin
Result := 0;
Result := Result + FPublicInt;
Result := Result + FProtectedInt;
Result := Result + FPrivateInt; // <- compiler error here
end;
另一方面,XE8 编译器报告错误“E2003 未声明的标识符 'FPrivateInt'。鉴于私有成员的可见性受限,这是我直觉上所期望的 在声明类的单元之外,如果我没有看到 Marco Cantu 的 Delphi 2007 Handbook of a class helper p89/90 上的示例 它访问“帮助”类的私有字段,也是一个明确的声明 在这个问题的公认答案的开头段落中
Can I call static private class method with class helper?
这似乎支持它:“众所周知,助手确实破解了私有可见性。因此,私有成员对类助手是可见的。......”
那么,为什么我会收到 E2003 Undeclared Identifier 错误?在我的理解或代码中,我显然在某处遗漏了一些东西。我在使用 XE4 和 XE6 时遇到了同样的错误,顺便说一句,XE4 早于我引用的 SO 答案,这是从去年开始的。
【问题讨论】:
-
你可以访问私有方法,而不是字段。
-
@TLama:Marco Cantu 的示例肯定使用了字段。我有它在我面前。
-
它可以使用members这个词,但是那里的所有代码(以及文本的意图)都演示了使用私有方法。还请查看 LURD 对同一问题的回答中引用的所有文档,所有这些都指私有 methods 或 functions。
-
@DavidHeffernan:Arf。当然,我把它改成了备用腮红。
-
@Ken 如果您错过了解决方案,请注意,成员一词的使用是准确且有意的。私有字段,就像助手中的私有方法一样可见。
标签: delphi helper delphi-xe8