delphi - 在子类中添加重载

标签 delphi overloading

我有一个基类 TParent它定义了一个没有 overload 的方法指示:

  TParent = class
  public
    procedure Test();
  end;

在子类中 TChild ,我正在尝试为 Test 添加重载程序。
  TChild = class(TParent)
  public
    procedure Test(AParam : Integer);
  end;

在编译时没有错误也没有警告但如果我尝试使用 TParent.TestTChild例如,它给出了一个 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/

相关文章:

delphi - 在Delphi中创建一个选项表格

delphi - Advantage 表文件在使用中出错。我该如何解决?

java - Java中接受两种不同类型作为参数的方法

c++ - 通过引入不相关的调用运算符奇怪地解决了模棱两可的调用运算符重载 - clang vs gcc

c++ - 逻辑 : B contained by set A 的模板和 C++ 运算符

java - 使用可变参数 (varargs) 的方法重载

delphi - 通过SQL语句和adoquery在delphi中删除数据库

Delphi:可维护性虚拟与虚拟抽象

delphi - Mac OS和Windows上的Delphi XE2静态数组分配不同

asp.net-mvc - 如何在 .net mvc 中调用重载操作?