我有一个通用的类似列表的类 tMyList<B>
使用方法 Each() 迭代每个元素并调用匿名过程 tMyList<A>.enumProc
with paramenter - 当前类型 <B>
的项目。
我想将该类实现为接口(interface),以便更轻松地进行生命周期管理。
问题是我无法在 iMyList<A>
中声明 Each 方法接口(interface),因为tMyList<A>.enumProc
类型未知。据我所知接口(interface)不支持嵌套类型?
这是代码:
tMyList<B> = class;
iMyList<A> = interface
procedure each(enumProcedure: iMyList<A>.enumProc); // ERROR - Undeclared identifier: 'enumProc'
end;
tMyList<B> = class(tInterfacedObject, iMyList<B>)
type
enumProc = reference to procedure(item: iMyList<B>);
public
procedure each(enumProcedure: enumProc);
end;
*
在这种特殊情况下,实现枚举器不是一个选项
最佳答案
完成这项工作的唯一方法是在实现类之外定义过程类型。像这样:
type
IMyIntf<A> = interface;
TMyProc<A> = reference to procedure(Intf: IMyIntf<A>);
IMyIntf<A> = interface
procedure Foo(Proc: TMyProc<A>);
end;
TMyClass<A> = class(TInterfacedObject, IMyIntf<A>)
procedure Foo(Proc: TMyProc<A>);
end;
关于delphi - 在类正在实现的泛型接口(interface)中使用在泛型类中声明的嵌套类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18677084/