“<<"的目的是什么?(双小于)在 groovy [英] What is the purpose of &quot;&lt;&lt;&quot; (double less than) in groovy

查看:31
本文介绍了“<<"的目的是什么?(双小于)在 groovy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一些代码中看到我无法理解 <<就像下面的示例代码

I saw in some of the code which I am unable to understand the purpose of << like in the following sample code

def renderFiles(args) {
   def model = []
   args.each { fileArgs ->
      model << createModel(fileArgs.id,fileArgs.path,fileArgs.ext)
   }
   render view:'show', model: [files:model]
}

推荐答案

这是一个 leftShift 操作符.在 groovy 中实现了运算符重载,参见 这里新文档 供参考.在这种特殊情况下,将元素添加到列表操作是重载的.

This is a leftShift operator. In groovy operator overloading is implemented, see here and the new docs for reference . In this particular case adding an element to list operation is overloaded.

代替:

model.add(createModel(fileArgs.id,fileArgs.path,fileArgs.ext))

你可以简单地写:

<代码>模型<

这是一种速记.List 类还覆盖了其他运算符,例如plus(), minus().请参阅此处.

It's a kind of a shorthand. There are also other operators overridden for List class, e.g. plus(), minus(). See here.

这篇关于“<<"的目的是什么?(双小于)在 groovy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆