delphi - 扩展类时的抽象错误

标签 delphi

type
  TObjA = class
    a: string;
  end;
type
  worker<T> = interface
    function step1(v: integer): T;
    function step2(s: string): T;
  end;

type
  ImplA<T> = class(TInterfacedObject, worker<T>)
    function step1(v: integer): T;
    function step2(s: string): T; virtual; abstract;
  end;

type
  ImplB = class(ImplA<TObjA>)
    function step2(s: string): TObjA;
  end;
implementation


function ImplA<T>.step1(v: integer): T;
begin
  result := step2(IntToStr(v));
end;

function ImplB.step2(s: string): TObjA;
var
  r: TObjA;
begin
  r := TObjA.Create;
  r.a := 'step2 ' + s;
  result := r;
end;


我正在尝试根据此结构构建功能。我知道它可以在Java中使用,但目前我正在使用delphi 2010。
调用ImplB.step1(1)时收到抽象错误
我该如何解决?

最佳答案

由于未将function step2(s: string): TObjA;声明为override,因此会出现错误。

所以在

function ImplA<T>.step1(v: integer): T;
begin
  result := step2(IntToStr(v));
end;


它从step2而不是ImplA调用ImplB,正如您期望的那样

您也将返回类型从泛型对象更改为TObjA,编译器可能不喜欢这种方式,但是我没有Delphi的副本,该副本支持泛型来进行测试。

关于delphi - 扩展类时的抽象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15480597/

相关文章:

delphi - 在项目的主要单元中使用条件 - IDE 会破坏代码

windows - 如何将 UTC 日期时间转换为指定时区?

Linux 路径工具

delphi - 两个向量之间的角度

delphi - 线程不接收消息

delphi - 是否有用于获取英文时区名称的 Windows API 例程?

delphi - 在运行时重新分配数据源

delphi - 为什么 Delphi 7 在追加模式下打开文件时会在 ASCII 代码 14 之后截断文件?

delphi - 为什么 Delphi 编译器不针对重新定义的常量发出警告?

multithreading - delphi中如何释放线程