我正在尝试实现Spring 4 Delphi,并仅对接口进行编程,而不是类。然而,当您想要使用TObjectList时,这似乎是不可能的。
请考虑以下代码:
unit Unit1;
interface
uses
Spring.Collections,
Spring.Collections.Lists;
type
IMyObjParent = interface
['{E063AD44-B7F1-443C-B9FE-AEB7395B39DE}']
procedure ParentDoSomething;
end;
IMyObjChild = interface(IMyObjParent)
['{E063AD44-B7F1-443C-B9FE-AEB7395B39DE}']
procedure ChildDoSomething;
end;
implementation
type
TMyObjChild = class(TInterfacedObject, IMyObjChild)
protected
procedure ParentDoSomething;
public
procedure ChildDoSomething;
end;
{ TMyObj }
procedure TMyObjChild.ChildDoSomething;
begin
end;
procedure TMyObjChild.ParentDoSomething;
begin
end;
procedure TestIt;
var
LMyList: IList<IMyObjChild>;
begin
TCollections.CreateObjectList<IMyObjChild>;
//[DCC Error] Unit1.pas(53): E2511 Type parameter 'T' must be a class type
end;
end.
我知道我可以在上面的例子中将IMyObjChild更改为TMyObjChild,但如果我需要在另一个单元或表单中使用它,该怎么办?
仅编程到接口似乎太难或不可能,一旦你需要一个TObjectList。
烦死了... 有什么想法或帮助吗?