如何继承通用的虚方法?

Fab*_*ujo 7 delphi generics inheritance delphi-xe2

我有以下代码.我想覆盖基础列表的Notify方法,以便能够在修改列表时创建一个事件.

  TDescendantList = class(TObjectList<TSomeclass>)
  private
    <...>
  protected
    procedure Notify(const Value: T;
      Action: TCollectionNotification); override;
    <...>
  end;
Run Code Online (Sandbox Code Playgroud)

如果我把Value: T我在T上得到一个"未声明的标识符".

如果Value: TSomeClass我得到"通知声明"与以前的声明不同".

Notify是一种受保护的方法TObjectList<T: class>.此方法不会出现在XE2 IDE的重写列表中.

这是实现这个的一些方法,或者我需要使用另一种方法,因为这是一个众所周知的砖墙?

J..*_*... 15

如果您的后代类正在修复泛型类型,那么您必须使用该固定类型来代替T.在您的情况下:

protected
  procedure Notify(const Value: TSomeclass;
                   Action: TCollectionNotification); override;
Run Code Online (Sandbox Code Playgroud)

是声明此函数的正确方法.


错误 :

"通知"声明与之前的声明不同

令人遗憾的是Delphi RTL在不同单元中复制类型名称的情况.

单位System.Classes定义

TCollectionNotification = (cnAdded, cnExtracting, cnDeleting);
Run Code Online (Sandbox Code Playgroud)

System.Generics.Collections定义

TCollectionNotification = (cnAdded, cnRemoved, cnExtracted);
Run Code Online (Sandbox Code Playgroud)

几乎可以肯定你已经Generics.Collections宣布之前 Classes你在uses子句和编译器解决的不想要的版本TCollectionNotification.

为了解决这个问题,无论是重组的uses条款,以便Generics.Collections以后自带Classes 使用完全限定的类型名称,即:

  procedure Notify(const Value: TSomeClass;
    Action: Generics.Collections.TCollectionNotification); override;
Run Code Online (Sandbox Code Playgroud)

differs from previous declaration错误的教训是有条不紊地检查您的类型. 类型标识符上的Ctrl+ CLICK将带您进入编译器正在使用的类型的定义.