相关疑难解决方法(0)

如何在派生类型中使用泛型方法

这看起来相当简单,也许我只是缺少一些语法粘合...这是我的简单泛型(Delphi XE3)示例:

unit Unit1;

interface

uses
  generics.collections;

type

X = class
public
  Id: Integer;
end;

XList<T : X> = class( TObjectList<T> )
 function Find(Id: Integer) : T;
end;

Y = class(X)

end;

YList = class(XList<Y>)
end;

implementation

{ XList<T> }

function XList<T>.Find(Id: Integer): T;
var
  t: X;
begin
  for t in Self do
    if t.Id = Id then
      Result := t;
end;

end.
Run Code Online (Sandbox Code Playgroud)

这不会用"[dcc32错误] Unit1.pas(41)编译:E2010不兼容的类型:'Y'和'X'".这是下线:

YList = class(XList<Y>)
end;
Run Code Online (Sandbox Code Playgroud)

Y来自X所以为什么会出现问题?

delphi generics inheritance delphi-xe3

7
推荐指数
2
解决办法
297
查看次数

泛型,多态,接口:解决方案是什么?

我知道标题非常广泛 - 跨越很多!

而且我希望这个问题可能演变成一个更大的"信息wiki thingy".

我学到了什么 - 到目前为止:

  • 使用泛型时 - 理解概念(协方差和逆变).
  • 不要"误用"泛型与继承相结合的概念.我做了,它可以让你直接陷入协方差问题!确保在继承中的正确位置"中断"泛型 - 如果要将两者结合起来.

(请纠正我 - 如果你认为我错了,错过或误解了什么).

我的问题是:

但到现在为止,我花了无数个小时,想弄清楚,如何解决我在桌面上的这个"大难题".而且我已经从你们这些SO用户那里得到了一些很好的答案 - 但是现在是时候让更大规模的工作了.

我冒昧地使用这个: Generics和Polymorphism一起工作

现在我有点困在这一点: 泛型不起作用的情况

为什么我最终得到协方差问题 - 是因为我的层次结构中的类过程.

所以我想知道Interfaces是否是我在这个"传奇"中的下一个大胆举动.如何"跨越"协方差问题.有一件事是发现你确实遇到了这个问题 - 另一件事是"如何解决它".

因此,如果你们中的任何一个好人"在那里"对此有任何意见 - 我都是耳朵.基本上:告诉我去接口(我从来没有从头开始做过).或者......按照你建议的方向给我一块骨头.

我当前的源池如第二个链接所述 - 从顶部开始.

这是我之前发布的一个小片段,显示了我的协方差问题. 大卫好心地解释了 - 为什么我跑进灌木丛......但现在我需要信息 - 如何绕过它.

var    
  aList : TBaseList<TBaseObject>;  // used as a list parameter for methods
  aPersonList : TPersonList<TPerson>;
  aCustomerList : TCustomerList<TCustomer>;
begin
  aPersonList := TPersonList<TPerson>.Create;
  aCustomerList := TCustomerList<TCustomer>.Create;

  aList := aCustomerList;  <-- this FAILS …
Run Code Online (Sandbox Code Playgroud)

delphi generics polymorphism covariance delphi-xe2

6
推荐指数
1
解决办法
513
查看次数

如何正确地使接口支持迭代?

我如何从接口公开这个TList,IEnumerator或者IEnumerator<IFungibleTroll>?我正在使用Delphi XE.

这是我有多远:

unit FungibleTrollUnit;

interface

uses
  Windows, Messages, SysUtils,
  Variants, Classes, Graphics,
  Controls, Forms,
  Generics.Collections;

type
  IFungibleTroll = interface
    ['{03536137-E3F7-4F9B-B1F5-2C8010A4D019}']
       function GetTrollName:String;
       function GetTrollRetailPrice:Double;
  end;


  TFungibleTrolls = class (TInterfacedObject,IEnumerable<IFungibleTroll>)
      protected
         FTrolls:TList<IFungibleTroll>;

      public
          // IEnumerable
          function GetEnumerator:IEnumerator<IFungibleTroll>;//
//          function GetEnumerator:IEnumerator; overload;

         // find/search app feature requires searching.
         // this

         function FindSingleItemByName(aName:String;patternMatch:Boolean):IFungibleTroll;
         function FindMultipleItemsByName(aName:String;patternMatch:Boolean):IEnumerable<IFungibleTroll>;
         function FindSingleItemByIdentifier(anIdentifer:String):IFungibleTroll;// use internal non-visible identifier to find an app.

         constructor Create;

         property Trolls:TList<IFungibleTroll> read FTrolls; // implements IEnumerable<IFungibleTroll>;??
  private
  end;




implementation


{ …
Run Code Online (Sandbox Code Playgroud)

delphi generics interface list

5
推荐指数
1
解决办法
2942
查看次数