是否可以在Delphi中使用Fluent调用样式自引用记录?
发布时间:2020-12-15 09:38:03 所属栏目:大数据 来源:网络整理
导读:目标是创建一个名为TURLString的类型,如下所示: var newURl : TURLString;begin newURL.Append('http://').Append('www.thehost.com').Append('path/on/server').Append('?'); ...lots of app logic... newURL.AppendParam('name','value').Append('#').App
目标是创建一个名为TURLString的类型,如下所示:
var newURl : TURLString; begin newURL.Append('http://').Append('www.thehost.com').Append('path/on/server').Append('?'); ...lots of app logic... newURL.AppendParam('name','value').Append('#').AppendParam('name','value'); ...more params added... result := httpClient.Get(newURL); end; 使用TURLString定义(注意它的记录): //from actual code used TURLString = record private FString : string; public function Append(APart : string) : TURLString; function AppendParam(AParam,AValue : string) : TURLString; end; function TURLString.Append(APart: string) : TURLString; begin FString := FString + APart; result := self; end; function TURLString.AppendParam(AParam,AValue: string): TURLString; begin if (not Empty) then FString := FString + URL_AMB; FString := FString + AParam + '=' + AValue; result := self; end; 当单步执行流体调用时,会附加值,但退出时它们将恢复为传递给第一个追加调用的第一个字符串,newURL等于’http://’,同时调试您看到的附加调用’http:// www .thehost.com /路径/ /服务器?名称=值#NAME =价值”. 这个概念是否可以带有记录? 解决方法
如果使用类似记录的值类型,则需要将最终返回的结果分配给变量:
newURL := newURL.Append('http://').Append('www.thehost.com'); 如果您使用类实例之类的引用类型,则可以使用您在问题中使用的语法. 引用类型方法将数据类型视为可变,而对于值类型,最好实现不可变数据类型. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |