procedure CaseListMyShares(search: String);
我有这样的程序。这是内容:
procedure TFormMain.CaseListMyShares(search: String);
var
i: Integer;
begin
myShares := obAkasShareApiAdapter.UserShares('1', search, '', '');
MySharesGrid.RowCount:= Length(myShares) +1;
MySharesGrid.AddCheckBoxColumn(0, false);
MySharesGrid.AutoFitColumns;
for i := 0 to Length(myShares) -1 do
begin
MySharesGrid.Cells[1, i+1]:= myShares[i].patientCase;
MySharesGrid.Cells[2, i+1]:= myShares[i].sharedUser;
MySharesGrid.Cells[3, i+1]:= myShares[i].creationDate;
MySharesGrid.Cells[4, i+1]:= statusText[StrToInt(myShares[i].situation) -1];
MySharesGrid.Cells[5, i+1]:= '';
MySharesGrid.Cells[6, i+1]:= '';
end;
end;
我想以两种没有任何参数和参数的方式调用此功能。我找到了过程的overload
关键字,但我不想两次编写相同的功能。
如果我称此过程为CaseListMyShares('');
,则可以使用。
,但是我可以在Delphi中的此操作吗?
procedure TFormMain.CaseListMyShares(search = '': String);
并致电:
CaseListMyShares();
有两种方法可以实现这一目标。两种方法都是有用的,并且通常可以互换。但是,在某些情况下,一个或另一个是可取的,因此值得知道以下两种技术。
默认参数值
的语法如下:
procedure DoSomething(Param: string = '');
您可以调用这样的方法:
DoSomething();
DoSomething('');
上述两种行为都以相同的方式行为。确实,当编译器遇到DoSomething()
时,它只是替换默认参数值并将代码编译好,就好像您已经写了DoSomething('')
。
文档:默认参数。
超载方法
procedure DoSomething(Param: string); overload;
procedure DoSomething; overload;
这些方法将像以下内容一样实现:
procedure TMyClass.DoSomething(Param: string);
begin
// implement logic of the method here
end;
procedure TMyClass.DoSomething;
begin
DoSomething('');
end;
请注意,逻辑的主体仍然仅实现一次。当以这种方式编写过载时,将有一个 master 方法执行工作,还有许多其他的过载来调用一个主方法。`
文档:超载过程和功能。