5

我想创建一个TObjectList<T>后代来处理我的应用程序中对象列表之间的通用功能。然后我想从那个新类中进一步下降,以便在需要时引入额外的功能。我似乎无法使用超过 1 级的继承来使其工作。我可能需要更多地了解泛型,但我已经到处寻找正确的方法来做到这一点,但没有成功。到目前为止,这是我的代码:

unit edGenerics;

interface

uses
  Generics.Collections;

type

  TObjectBase = class
  public
    procedure SomeBaseFunction;
  end;

  TObjectBaseList<T: TObjectBase> = class(TObjectList<T>)
  public
    procedure SomeOtherBaseFunction;
  end;

  TIndexedObject = class(TObjectBase)
  protected
    FIndex: Integer;
  public
    property Index: Integer read FIndex write FIndex;
  end;

  TIndexedObjectList<T: TIndexedObject> = class(TObjectBaseList<T>)
  private
    function GetNextAutoIndex: Integer;
  public
    function Add(AObject: T): Integer;
    function ItemByIndex(AIndex: Integer): T;
    procedure Insert(AIndex: Integer; AObject: T);
  end;

  TCatalogueItem = class(TIndexedObject)
  private
    FID: integer;
  public
    property ID: integer read FId write FId;
  end;

  TCatalogueItemList = class(TIndexedObjectList<TCatalogueItem>)
  public
    function GetRowById(AId: Integer): Integer;
  end;

implementation

uses
  Math;

{ TObjectBase }

procedure TObjectBase.SomeBaseFunction;
begin
end;

{ TObjectBaseList<T> }

procedure TObjectBaseList<T>.SomeOtherBaseFunction;
begin
end;

{ TIndexedObjectList }

function TIndexedObjectList<T>.Add(AObject: T): Integer;
begin
  AObject.Index := GetNextAutoIndex;
  Result := inherited Add(AObject);
end;

procedure TIndexedObjectList<T>.Insert(AIndex: Integer; AObject: T);
begin
  AObject.Index := GetNextAutoIndex;
  inherited Insert(AIndex, AObject);
end;

function TIndexedObjectList<T>.ItemByIndex(AIndex: Integer): T;
var
  I: Integer;
begin
  Result := Default(T);
  while (Count > 0) and (I < Count) and (Result = Default(T)) do
    if Items[I].Index = AIndex then
      Result := Items[I]
    else
      Inc(I);
end;

function TIndexedObjectList<T>.GetNextAutoIndex: Integer;
var
  I: Integer;
begin
  Result := 0;
  for I := 0 to Count - 1 do
    Result := Max(Result, Items[I].Index);
  Inc(Result);
end;

{ TCatalogueItemList }

function TCatalogueItemList.GetRowById(AId: Integer): Integer;
var
  I: Integer;
begin
  Result := -1;
  for I := 0 to Pred(Self.Count) do
    if Self.Items[I].Id = AId then
    begin
      Result := I;
      Break;
    end;
end;

end.
/////// ERROR HAPPENS HERE ////// ???? why is beyond me

似乎以下声明:

>>> TCatalogueItemList = class(TIndexedObjectList<TCatalogueItem>) <<<<

导致以下编译器错误:

[DCC 错误] edGenerics.pas(106):E2010 不兼容的类型:“TCatalogueItem”和“TIndexedObject”

然而,编译器在编译单元的末尾(第 106 行)显示错误,而不是在声明本身上,这对我来说没有任何意义......

基本上我的想法是我有一个从 TObjectList 下降的通用列表,我可以根据需要扩展新功能。对此的任何帮助都会很棒!!!

我应该使用 Delphi 2010 添加。

谢谢。

4

1 回答 1

9

您的错误在于类型转换,并且编译器错误正常(但它无法在我的 Delphi XE3 中找到正确的文件)。

您的 ItemByIndex 方法已声明:

TIndexedObjectList<T>.ItemByIndex(AIndex: Integer): T;

但是你有这行:

Result := TIndexedObject(nil);

这对于函数的结果是类型的父类很好,但对于函数的结果是类型后代类不行了。TIndexedObjectListTIndexedObject TCatalogueItemListTCatalogueItem

您可能知道,TCatalogueItem实例是与变量兼容的TIndexedObject赋值,但反之则不然。它翻译成这样的:

function TCatalogueItemList.ItemByIndex(AIndex: Integer): TCatalogueItem;
begin
  Result := TIndexedObject(nil);  //did you see the problem now?

要将结果初始化为 nil 值,可以调用 Default() 伪函数,如下所示:

Result := Default(T);

在 Delphi XE 或更高版本中,该解决方案也是通用的。不是将结果类型转换为固定TIndexedObjectList类,而是使用 T 类型应用泛型类型转换

Result := T(nil);
//or 
Result := T(SomeOtherValue);

但是,在这种特定情况下,不需要对常量进行类型转换nil,因为nil它是一个与任何引用都兼容的特殊值,因此您只需将该行替换为:

Result := nil;

它将编译,并希望按您的预期工作。

于 2013-02-21T02:38:48.610 回答