delphi – 如何在重写的虚函数中调用`Inherited`祖先方法?
发布时间:2020-12-15 03:51:41 所属栏目:大数据 来源:网络整理
导读:这有效: constructor TMyObj.Create;begin inherited;end; 为什么这也不起作用? function TMyObjEx.Import(CONST FileName: string; CONST x,y,z: Integer): string;begin result:= inherited; // Import(FileName,x,z); --- Compiler says: "incompatible
这有效:
constructor TMyObj.Create; begin inherited; end; 为什么这也不起作用? function TMyObjEx.Import(CONST FileName: string; CONST x,y,z: Integer): string; begin result:= inherited; // Import(FileName,x,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,z: Integer): string; virtual; end; TMyObjEx= class(TMyObj) private protected public function Import(CONST FileName: string; CONST x,z: Integer): string; override; end; 解决方法
这是正确的答案.
正如您在上面提到的那样,正确的方法是: function TMyObjEx.Import(CONST FileName: string; CONST x,z: Integer): string; begin result:= inherited Import(FileName,z); //do other stuff here end; 语言不支持您希望这样做的方式. 所以最终回答你的问题“为什么这不起作用?”是因为这不是语言的设计方式. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |