gpt4 book ai didi

class - 使用类引用进行多态和继承(第2部分)?

转载 作者:行者123 更新时间:2023-12-03 19:07:58 25 4
gpt4 key购买 nike

下面的控制台应用程序显示“运行时错误” ...

为什么会发生这种情况?非常感谢 !

PS:Related SO post

program Project2;

{$APPTYPE CONSOLE}

type
TParent = class;
TParentClass = class of TParent;

TParent = class
public
procedure Work; virtual; abstract;
end;

TChild1 = class(TParent)
public
procedure Work; override;
end;

TChild2 = class(TParent)
public
procedure Work; override;
end;

procedure TChild1.Work;
begin
WriteLn('Child1 Work');
end;

procedure TChild2.Work;
begin
WriteLn('Child2 Work');
end;

procedure Test(ImplClass: TParentClass);
var
ImplInstance: TParent;
begin
ImplInstance := ImplClass.Create;
ImplInstance.Work;
ImplInstance.Free;
end;

begin
Test(TParent);
Test(TChild1);
Test(TChild2);
Readln;
end.

最佳答案

TParent.Work方法声明为abstractdocumentation说:


您只能在已重写该方法的类或类的实例中调用抽象方法。


调用TParent.Work时,您违反了该规则,因此遇到运行时错误。

关于class - 使用类引用进行多态和继承(第2部分)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23628251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com