开发者

Delphi - TInterfaceList descendant to support more types - Abstract Error

I have a class TRow = class(TInterfaceList) where the items I want to add are descendants from IField (TFieldType1 and TFieldType2) I have a method where I want to iterate through the items and call an IField method on them:

(Item[i] as IField).IFieldMethod

And I am getting Abs开发者_运维知识库tract Error? What am I doing wrong? Am I wrong in my OOP understanding or Delphi?

Thank you


You've got a class implementing IField.IFieldMethod using a abstract virtual method, and you ignored a Constructing instance of <TType> containing abstract method <MethodName>.

Here's a short console demo of code that exhibits your error. The fact that you're calling IFieldMethod using (Item[i] as IField).IFieldMethod doesn't provide any new information, it only hides the cause of the problem. Store the IField type interface reference to a local variable and you've got my code:

program Project23;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Classes;

type IDummyIntf = interface
  procedure DoStuff;
end;

type TDummyImp = class(TInterfacedObject, IDummyIntf)
  procedure DoStuff;virtual;abstract; // TDummyImp implements IDummyIntf.DoStuff using a VIRTUAL ABSTRACT method.
end;

var X: IDummyIntf;
begin
  X := TDummyImp.Create; // <-- Warning at this line, Constructing instance of TDummyImp containing abstract method TDummyImp.DoStuff 
  X.DoStuff; // This raises EAbstractError because TDummyImp doesn't actually implement DoStuff
  ReadLn;
end.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜