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

Delphi中的Const函数

发布时间:2020-12-15 09:11:16 所属栏目:大数据 来源:网络整理
导读:在我看到的Delphi代码中,我发现了以下几行: const function1: function(const S: String): String = SomeVariable1; function2: function(const S: String): String = SomeVariable2; 这是做什么的?我的意思是,不是函数中的实际代码,而是如何在const部分中
在我看到的Delphi代码中,我发现了以下几行:

const
    function1: function(const S: String): String = SomeVariable1;
    function2: function(const S: String): String = SomeVariable2;

这是做什么的?我的意思是,不是函数中的实际代码,而是如何在const部分中声明一个函数并将它(?)与变量值进行比较?我假设单个等于是比较,因为这就是Delphi中的其他地方.

谢谢.

解决方法

不,等于是赋值,因为这是常量的赋值方式.例如,考虑一下

const Pi = 3.1415;

要么

const s = 'This is an example';

还有’键入的常量’:

const Pi: extended = 3.1415;

在上面的代码片段中,我们定义了一个包含签名函数函数的类型常量(const S:String):String.我们为它分配(兼容)函数SomeVariable1.

必须先在代码中定义SomVariable1,例如,

function SomeVariable1(const S: String): String;
begin
  result := S + '!';
end;

请考虑以下示例:

function SomeVariable1(const S: String): String;
begin
  result := S + '!';
end;

const
  function1: function(const S: String): String = SomeVariable1;

procedure TForm1.FormCreate(Sender: TObject);
begin
  caption := function1('test');
end;

(编辑:李大同)

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

    推荐文章
      热点阅读