Delphi能不能做以下的宣告?
TDictionary <T, TList <T>>
編譯器不喜歡它:
未宣告的識別符號:'T'
。我在uses子句中加入了:
System.Generics.Collections;
更新:使用這段代碼,我遇到了這些問題:
interface
使用
System.Generics.Collections。
型別
TListado = class(TObject)
private[/span
串列。TDictionary<T, V: TList<T> >。
function GetListado: TDictionary<T,TList<T> >;
procedure SetListado(const Value: TDictionary<T, TList<T>>);
public
property Listado: TDictionary<T,TList<T>> read GetListado write SetListado;
function ReadItems(Cliente: T):TList<T> 。
end。
我改變了單元代碼,但在這之前它是有效的,我不知道我在哪里失敗了。
未宣告的識別符號:'T'
。
uj5u.com熱心網友回復:
你似乎對泛型的作業原理有一個根本的誤解。 我強烈建議你更仔細地閱讀檔案。
您正試圖在需要類的具體實體的情況下使用 TDictionary。在你所展示的代碼中,編譯器認為 T 是一個未知的型別,可以用來實體化你對 TDictionary 的使用。
在你使用 T 的地方,你需要指定一個實際的型別,你想用這個字典來使用,例如:
interface
使用
System.Generics.Collections。
型別
TListado = class(TObject)
private[/span
FListado: TDictionary<Integer, TList<Integer> >。
function GetListado: TDictionary<Integer, TList<Integer> >;
procedure SetListado(constValue: TDictionary<Integer, TList<Integer>>);
public
property Listado: TDictionary<Integer, TList<Integer>> read GetListado write SetListado;
function ReadItems(Cliente: Integer): TList<TInteger> 。
end。
否則,你需要將TListado本身宣告為一個具有自己引數的通用類,然后你可以用它來實體化TDictionary,然后你可以在實體化TListado時為該引數指定一個型別,例如:
interface。
使用
System.Generics.Collections。
型別
TListado<T> = class(TObject)
private[/span
串列。TDictionary<T, TList<T>> 。
function GetListado: TDictionary<T, TList<T> >;
procedure SetListado(constValue: TDictionary<T, TList<T>>);
public
property Listado: TDictionary<T, TList<T>> read GetListado write SetListado;
function ReadItems(Cliente: T): TList<T> 。
end。
var.
串列。TListado<Integer>。
begin
list := TListado<Integer>.Create;
...
list.Free;
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/329628.html
標籤:
