gpt4 book ai didi

delphi - Delphi 2010 Generics of Generics

转载 作者:行者123 更新时间:2023-12-03 18:39:36 30 4
gpt4 key购买 nike

如何将泛型存储在非泛型对象持有的泛型TList中?

type
TXmlBuilder = class
type
TXmlAttribute<T>= class
Name: String;
Value: T;
end;

TXmlNode = class
Name: String;
Attributes: TList<TXmlAttribute<T>>;
Nodes: TList<TXmlNode>;
end;
...
end;


编译器说T不在

Attributes: TList<TXmlAttribute<T>>;


-
皮埃尔·雅格

最佳答案

TXmlNode不知道什么是T。应该是什么?

也许你的意思是:

TXmlNode<T> = class
Name: String;
Attributes: TList<TXmlAttribute<T>>;
Nodes: TList<TXmlNode<T>>;
end;


...或者,或者您需要指定类型。

但是,您似乎在这里缺少了一些东西。泛型允许您为每种类型创建一个单独的类,而不是为所有类型创建一个类。在上面的代码中,TList包含一个相同类型的数组,您可能希望它们不同。考虑一下这个:

  TXmlBuilder = class
type
TXmlAttribute= class
Name: String;
Value: Variant;
end;

TXmlNode = class
Name: String;
Attributes: TList<TXmlAttribute>;
Nodes: TList<TXmlNode>;
end;
...
end;

关于delphi - Delphi 2010 Generics of Generics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2138800/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com