我刚刚阅读了一种有趣的构造代码的方法,它隐藏了单元实现部分中的实际类,并且仅使用接口公开它们,如下所示:
unit MyClass;
interface
type
IMyClass = interface(IInterface)
[GUID]
procedure A;
procedure B;
end;
function CreateMyClass: IMyClass;
implementation
type
TMyClass = class(TInterfaceObject, IMyClass)
strict private
procedure A;
procedure B;
end;
function CreateMyClass: IMyClass;
begin
Result := TMyClass.Create;
end;
...
end;
在我需要从TMyClass继承一个代码重用代码之前,这会产生奇迹。除了在同一单元implementation
部分中插入第二个类之外,还有办法吗?
答案 0 :(得分:1)
您可以从类继承的唯一方法是可以看到类声明的代码。因此,如果声明仅出现在实现部分中,则只有该实现部分中的代码才能从该类派生。