我想实现Spring 4德尔福和只有程序接口,而不是类。然而,当你想使用TObjectList时,这似乎是不可能的。德尔福接口和IList <T>(或TObjectList <T>)
考虑下面的代码:
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,试图编程接口似乎太难或不可能。
Grrr ...任何想法或帮助?
您可以使用TInterfaceList。 –
如果使用接口,则不需要TObjectList,因为它在TList上的所有操作都管理它所持有的对象的生命周期。 –
谢谢!我已经为此苦苦挣扎了好几天,但由于接口是引用计数,所以TList可以用来代替TObjectList。 –