与我的最后一个问题有关,我现在有以下问题:
function TNodeFactory <T>.CreateNode (ID : Integer) : INodeInterface <T>;
var
NodeClass : TClass;
begin
NodeClass := FindRegisteredClass (ID);
Result := NodeClass.Create;
end;
这会产生编译器错误:
E2010 Incompatible Types: 'INodeInterface<TNodeFactory<T>.T>' and 'TObject'
演员表也不起作用。
我在这里想念什么?
编辑:当前实施
TNodeFactory <T> = class
private
type
TRegisteredNodeType = record
ID : Integer;
NodeClass : TClass;
end;
private
FNodeTypeList : TList <TRegisteredNodeType>
public
procedure RegisterNodeType (ID : Integer; NodeClass : TClass);
function CreateNode (ID : Integer) : INodeInterface <T>;
end;
procedure TNodeFactory <T>.RegisterNodeType (ID : Integer; NodeClass : TClass);
var
RegisteredType : TRegisteredNodeType;
begin
RegisteredType.ID := ID;
RegisteredType.NodeClass := NodeClass;
FNodeTypeList.Add (RegisteredType);
end;
function TNodeFactory <T>.CreateNode (ID : Integer);
var
RegisteredType : TRegisteredNodeType;
begin
for RegisteredType in FNodeTypeList do
if (RegisteredType.ID = ID) then
Exit (RegisteredType.NodeClass.Create);
raise EInvalidNodeType.Create ('No node type with ID ' + IntToStr (ID) + ' registered');
end;
(简化和删除的错误检查)