SOU*_*ser 1 delphi polymorphism inheritance reference class
下面的控制台应用程序给出了"运行时错误"......
为什么会这样?非常感谢 !
PS:相关SO帖子
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.
Run Code Online (Sandbox Code Playgroud)