我创建了两个单元并将第一个类放入其中一个:
unit UBaseClass; interface type TBaSEOuterClass = class protected type TBaseInnerClass = class public end; protected function GetInnerInstance: TBaSEOuterClass.TBaseInnerClass; virtual; end; implementation { TBaSEOuterClass } function TBaSEOuterClass.GetInnerInstance: TBaSEOuterClass.TBaseInnerClass; begin // doesn't matter end; end.
我将派生类放入第二个单元:
unit UDerClass; interface uses UBaseClass; type TDerOuterClass = class(TBaSEOuterClass) protected type TDerInnerClass = class(TBaseInnerClass) end; protected function GetInnerInstance: TBaSEOuterClass.TBaseInnerClass; override; end; implementation { TDerOuterClass } function TDerOuterClass.GetInnerInstance: TBaSEOuterClass.TBaseInnerClass; begin end; end.
当我想要编译时,我得到了
[dcc32 Error] UDerClass.pas(22): E2362 Cannot access protected symbol
TBaSEOuterClass.TBaseInnerClass
在行函数TDerOuterClass.GetInnerInstance:TBaSEOuterClass.TBaseInnerClass;
我无法理解为什么TBaSEOuterClass.TBaseInnerClass(作为内部受保护类)无法从TDerOuterClass(为TBaSEOuterClass派生)访问.在这种情况下,实际上受保护的类型是什么?
我在Nested Type Declarations topic没有找到任何解释.所以有这种行为的原因吗?
它也与简单的受保护类型相关
protected type TSimpleType = Integer;
我无法在TDerOuterClass中编写函数
protected function GetValue: TSimpleType;
因为我会收到一条消息
[dcc32 Error] UDerClass.pas(16): E2003 Undeclared identifier:
‘TSimpleType’
解决方法
它看起来像一个bug.我建议把它发布到
quality portal.
现在你可以声明类型别名来欺骗编译器(在XE7中测试).
unit UDerClass; interface uses UBaseClass; type TDerOuterClass = class(TBaSEOuterClass) protected type TBaseInnerClass = TBaSEOuterClass.TBaseInnerClass; // <= type alias to avoid compiler error TDerInnerClass = class(TBaseInnerClass) end; protected function GetInnerInstance: TBaseInnerClass; override; end; implementation { TDerOuterClass } function TDerOuterClass.GetInnerInstance: TBaseInnerClass; begin Result := TDerInnerClass.Create; end; end.