0

德尔福可以做出以下陈述吗?

TDictionary <T, TList <T>>

编译器不喜欢它:

未声明的标识符:'T'

我在uses条款中添加了:

System.Generics.Collections;

更新:使用此代码我有这些问题:

interface

uses
  System.Generics.Collections;

type
  TListado = class(TObject)
  private
    FListado: 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'

4

1 回答 1

4

您似乎对泛型的工作方式存在根本性的误解。我强烈建议您更仔细地阅读文档。

您正在尝试TDictionary在需要类的特定实例化的上下文中使用。在您显示的代码中,编译器是正确的,它T是一个未知类型,可用于实例化您对TDictionary.

在您使用的任何地方T,您都需要指定要与字典一起使用的实际类型,例如:

interface

uses
  System.Generics.Collections;

type
  TListado = class(TObject)
  private
    FListado: TDictionary<Integer, TList<Integer>>;
    function GetListado: TDictionary<Integer, TList<Integer>>;
    procedure SetListado(const Value: TDictionary<Integer, TList<Integer>>);
  public
    property Listado: TDictionary<Integer, TList<Integer>> read GetListado write SetListado;
    function ReadItems(Cliente: Integer): TList<TInteger>;
  end; 

否则,您需要将自己声明TListado为具有自己的参数的 Generic 类,然后您可以使用它来实例化TDictionary,然后您可以在实例化时为该参数指定类型TListado,例如:

interface

uses
  System.Generics.Collections;

type
  TListado<T> = class(TObject)
  private
    FListado: TDictionary<T, 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; 
var
  list: TListado<Integer>;
begin
  list := TListado<Integer>.Create;
  ...
  list.Free;
end;
于 2021-09-19T18:15:46.390 回答