加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Delphi过程参数的默认值

发布时间:2020-12-15 03:55:37 所属栏目:大数据 来源:网络整理
导读:procedure CaseListMyShares(search: String); 我有这样的程序.这是内容: procedure TFormMain.CaseListMyShares(search: String);var i: Integer;begin myShares := obAkasShareApiAdapter.UserShares('1',search,'',''); MySharesGrid.RowCount:= Length(
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;

我想用两种方式调用这个函数,它没有任何参数和参数.我发现程序的重载关键字,但我不想写两次相同的功能.

如果我像CaseListMyShares(”);那样调用这个过程,它就可以了.

但我可以在delphi中执行此操作吗?

procedure TFormMain.CaseListMyShares(search = '': String);

并致电:

CaseListMyShares();

解决方法

有两种方法可以实现这一目标.这两种方法都很有用,它们通常是可以互换的.然而,有些情况下,其中一个或另一个是优选的,因此值得了解以下两种技术.

默认参数值

其语法如下:

procedure DoSomething(Param: string = '');

您可以像这样调用方法:

DoSomething();
DoSomething('');

以上两者都以相同的方式表现.实际上,当编译器遇到DoSomething()时,它只是替换默认参数值并编译代码,就像编写了DoSomething(”)一样.

文档:Default Parameters.

重载方法

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;

请注意,逻辑主体仍然只实现一次.以这种方式编写重载时,将有一个执行工作的主方法,以及调用该主方法的许多其他重载.

文件:Overloading Procedures and Functions.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读