我有一个基类 TParent
它定义了一个没有 overload
的方法指示:
TParent = class
public
procedure Test();
end;
在子类中
TChild
,我正在尝试为 Test
添加重载程序。 TChild = class(TParent)
public
procedure Test(AParam : Integer);
end;
在编译时没有错误也没有警告但如果我尝试使用
TParent.Test
与 TChild
例如,它给出了一个 E2035 错误,就像父方法被子方法隐藏一样:var
Obj : TChild;
begin
Obj := TChild.Create;
try
Obj.Test();
finally
Obj.Free;
end;
end;
[dcc32 Error] Unit1.pas(52): E2035 Not enough actual parameters
为了解决编译错误,我必须添加
overload
TChild.Test
的指令宣言。 TChild = class(TParent)
public
procedure Test(AParam : Integer); overload;
end;
它可以编译并且似乎可以工作,但是即使
TParent.Test
也是正确的声明根本没有重载指令?或者我应该更改子程序的名称,以防父类没有预测方法被重载?
最佳答案
我相信这完全没问题。
overloading methods 上的文档(而不是简单的 overloading non-method procedures and functions )状态
A method can be redeclared [in a descendant class (my remark)] using the
overload
directive. In this case, if the redeclared method has a different parameter signature from its ancestor, it overloads the inherited method without hiding it. Calling the method in a descendent class activates whichever implementation matches the parameters in the call.
这是唯一给出的描述,它不是很清楚。尽管如此,最合理的解释是按照你的建议去做是完全没问题的。我认为它可以工作并在许多现有的 Delphi 源代码中使用的事实解决了它。
关于delphi - 在子类中添加重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62321119/