如何继承通用的虚拟方法? [英] How to inherit generic virtual method?

查看:188
本文介绍了如何继承通用的虚拟方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码。我想重写基本列表的Notify方法,以便能够修改列表创建一个事件。

  TDescendantList = class(TObjectList< TSomeclass>)
private
< ...>
保护
过程通知(const值:T;
操作:TCollectionNotification);覆盖;
< ...>
end;

如果我把值:T 如果是值:TSomeClass 上的 Undeclared identifier code>我得到了 通知的声明与以前的声明 不同。

Notify TObjectList< T:class> 的受保护方法。这个方法不会出现在XE2 IDE的重载列表中。



这是实现这个方法的一种方式,或者我需要使用另一种方法,因为这是一个众所周知的砖墙?如果您的后代类正在修复泛型,那么您必须使用该固定类型来代替T.在您的情况下:

 保护
过程通知(常量值:TSomeclass;
操作:TCollectionNotification);覆盖;

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





错误:


'Notify'的声明不同于前面的声明 p>

是Delphi RTL复制类型名称在不同单元中的一个令人遗憾的例子。



单元 System.Classes 定义

  TCollectionNotification =(cnAdded,cnExtracting,cnDeleting); 

System.Generics.Collections 定义

  TCollectionNotification =(cnAdded,cnRemoved,cnExtracted); 

几乎可以肯定你已经声明了 Generics.Collections 中的类使用子句,编译器正在解析不需要的版本 TCollectionNotification



要解决它,要么重新组织你的使用子句因此在 Classes 后使用完全限定类型名称<= c $ c> Generics.Collections :

  procedure Notify(const Value:TSomeClass; 
Action:Generics.Collections.TCollectionNotification);覆盖;






使用不同于以前的声明错误是有条不紊地检查你的类型。类型标识符上的 Ctrl + CLICK 将引导您使用编译器正在使用的类型的定义。

I have the following code. I want to override the Notify method of the base base list on this to be able to create an event on the modification of the list.

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

If I put Value: T I get an "Undeclared identifier" on T.

If is Value: TSomeClass I get the "Declaration of 'Notify' differs from previous declaration".

Notify is a protected method of TObjectList<T: class>. This method does not appear on overriding list of the XE2 IDE.

That's some way to implement this or I need to use another approach as this is an proverbial brick wall?

解决方案

If your descendant class is fixing the generic type then you have to use that fixed type in place of T. In your case :

protected
  procedure Notify(const Value: TSomeclass;
                   Action: TCollectionNotification); override;

is the correct way to declare this function.


The error :

Declaration of 'Notify' differs from previous declaration

is a regrettable case of the Delphi RTL duplicating type names in different units.

The unit System.Classes defines

TCollectionNotification = (cnAdded, cnExtracting, cnDeleting);

and System.Generics.Collections defines

TCollectionNotification = (cnAdded, cnRemoved, cnExtracted);

Almost certainly you have Generics.Collections declared before Classes in your uses clause and the compiler is resolving the undesired version of TCollectionNotification.

To fix it, either reorganize your uses clauses so that Generics.Collections comes after Classes or use a fully qualified type name, ie :

  procedure Notify(const Value: TSomeClass;
    Action: Generics.Collections.TCollectionNotification); override;


The lesson with a differs from previous declaration error is to methodically check your types. Ctrl+CLICK on the type identifier will take you to the definition of the type the compiler is using.

这篇关于如何继承通用的虚拟方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆