在Delphi中,我们可以将一个被声明为
Translated: The following example shows the situation. Is it possible to pass an overloaded method as an anonymous method parameter?
procedure of object
的类方法作为匿名方法类型的参数进行赋值。对于重载方法(相同的方法名称),传递对象方法会导致编译错误:[dcc32 Error] Project56.dpr(40): E2010 Incompatible types: 'System.SysUtils.TProc<System.Integer,System.Integer>' and 'Procedure of object'
下面的例子展示了这种情况。是否可以让重载的方法作为匿名方法参数传递?Translated: The following example shows the situation. Is it possible to pass an overloaded method as an anonymous method parameter?
type
TTest = class
public
procedure M(a: Integer); overload;
procedure M(a, b: Integer); overload;
end;
procedure TTest.M(a, b: Integer);
begin
WriteLn(a, b);
end;
procedure TTest.M(a: Integer);
begin
WriteLn(a);
end;
procedure DoTask1(Proc: TProc<Integer>);
begin
Proc(100);
end;
procedure DoTask2(Proc: TProc<Integer,Integer>);
begin
Proc(100, 200);
end;
begin
var o := TTest.Create;
DoTask1(o.M); // <-- Success
DoTask2(o.M); // <-- Fail to compile: E2010 Incompatible types: 'System.SysUtils.TProc<System.Integer,System.Integer>' and 'Procedure of object'
ReadLn;
end.