gpt4 book ai didi

delphi - 如何在重写的虚函数中调用 `Inherited` 祖先方法?

转载 作者:行者123 更新时间:2023-12-03 14:45:09 24 4
gpt4 key购买 nike

这有效:

constructor TMyObj.Create;
begin
inherited;
end;

为什么这也不起作用?

function TMyObjEx.Import(CONST FileName: string; CONST x, y, z: Integer): string;
begin
result:= inherited; // Import(FileName, x, y, z); <--- Compiler says: "incompatible types"
//do other stuff here
end;

TMyObjEx 的声明如下:

TYPE

TMyObj = class(TChObj)
private
protected
public
function Import (CONST FileName: string; CONST x, y, z: Integer): string; virtual;
end;

TMyObjEx= class(TMyObj)
private
protected
public
function Import(CONST FileName: string; CONST x, y, z: Integer): string; override;
end;

最佳答案

这是正确答案。

执行此操作的正确方法如您上面所述:

function TMyObjEx.Import(CONST FileName: string; CONST x, y, z: Integer): string;
begin
result:= inherited Import(FileName, x, y, z);
//do other stuff here
end;

该语言不支持您想要的方式。

最终回答了“为什么这不起作用?”的问题。是因为这不是语言的设计方式。

关于delphi - 如何在重写的虚函数中调用 `Inherited` 祖先方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/340842/

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