fbpx
维基百科

方法链式调用

方法链式调用(Method chaining),也称为命名参数惯用法(named parameter idiom),是面向对象编程语言中多个方法被调用时的常用语法。每个方法都返回一个对象,允许在单个语句中将调用链接在一起,而无需变量来存储中间结果。[1]

方法链式调用是一种语法糖[2]

类似的语法是方法级联调用,即调用一个对象的多个方法的语法糖。方法级联调用可以使用方法链式调用来实现,即让每个方法返回当前对象本身英语this (computer programming)(this)。方法级联调用是流畅接口的一项关键技术,面向对象语言广泛实现了方法链式调用,但实现了方法级联调用的不多。链式和级联调用都来自 Smalltalk语言

虽然方法链式调用是语法,但它具有语义后果,即需要方法返回一个对象;如果通过方法链式调用实现级联,这必须是当前对象本身英语this (computer programming)。 这可以防止返回值被用于其他目的,例如返回错误值英语error value

例子 编辑

一个常见例子是C++标准模板库中的iostream,其中运算符<<返回左参数对象,因此允许链式调用:

比较:

a << b << c; 

等价于:

a << b; a << c; 

另一个例子是JavaScript使用内建的数组方法:

somethings  .filter(x => x.count > 10)  .sort((a, b) => a.count - b.count)  .map(x => x.name) 

参见 编辑

参考文献 编辑

  1. ^ . First Class Thoughts. [2011-04-13]. (原始内容存档于2011-02-22). In order to simplify repeated object interactions on the same object the old trick Method Chaining originating the world of Smalltalk should be enforced. The idea is to let methods return this rather than void, thus affecting especially set() and add() methods. Method chaining arose during the designers of Smalltalk pursuit to minimize the number of keywords in the language, which lead to the discovery that void is an unnecessary keyword!. 
  2. ^ Martin, Robert Cecil. Clean Code: A Handbook of Agile Software Craftsmanship. Prentice Hall. 2008. ISBN 0-13-235088-2. 

外部链接 编辑

方法链式调用, method, chaining, 也称为命名参数惯用法, named, parameter, idiom, 是面向对象编程语言中多个方法被调用时的常用语法, 每个方法都返回一个对象, 允许在单个语句中将调用链接在一起, 而无需变量来存储中间结果, 是一种语法糖, 类似的语法是方法级联调用, 即调用一个对象的多个方法的语法糖, 方法级联调用可以使用来实现, 即让每个方法返回当前对象本身, 英语, this, computer, programming, this, 方法级联调用是流畅接口的一项关键技. 方法链式调用 Method chaining 也称为命名参数惯用法 named parameter idiom 是面向对象编程语言中多个方法被调用时的常用语法 每个方法都返回一个对象 允许在单个语句中将调用链接在一起 而无需变量来存储中间结果 1 方法链式调用是一种语法糖 2 类似的语法是方法级联调用 即调用一个对象的多个方法的语法糖 方法级联调用可以使用方法链式调用来实现 即让每个方法返回当前对象本身 英语 this computer programming this 方法级联调用是流畅接口的一项关键技术 面向对象语言广泛实现了方法链式调用 但实现了方法级联调用的不多 链式和级联调用都来自 Smalltalk语言 虽然方法链式调用是语法 但它具有语义后果 即需要方法返回一个对象 如果通过方法链式调用实现级联 这必须是当前对象本身 英语 this computer programming 这可以防止返回值被用于其他目的 例如返回错误值 英语 error value 目录 1 例子 2 参见 3 参考文献 4 外部链接例子 编辑一个常见例子是C 标准模板库中的iostream 其中运算符 lt lt 返回左参数对象 因此允许链式调用 比较 a lt lt b lt lt c 等价于 a lt lt b a lt lt c 另一个例子是JavaScript使用内建的数组方法 somethings filter x gt x count gt 10 sort a b gt a count b count map x gt x name 参见 编辑流式接口 管道 Unix Nesting computing 英语 Nesting computing 生成器模式 诅咒金字塔 编程 参考文献 编辑 Applying Method Chaining First Class Thoughts 2011 04 13 原始内容存档于2011 02 22 In order to simplify repeated object interactions on the same object the old trick Method Chaining originating the world of Smalltalk should be enforced The idea is to let methods returnthisrather thanvoid thus affecting especiallyset andadd methods Method chaining arose during the designers of Smalltalk pursuit to minimize the number of keywords in the language which lead to the discovery thatvoidis an unnecessary keyword Martin Robert Cecil Clean Code A Handbook of Agile Software Craftsmanship Prentice Hall 2008 ISBN 0 13 235088 2 外部链接 编辑Creating DSLs in Java using method chaining concept 页面存档备份 存于互联网档案馆 Method Chaining in PHP 页面存档备份 存于互联网档案馆 取自 https zh wikipedia org w index php title 方法链式调用 amp oldid 74258383, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。