groovy闭包两个返回值举例
时间: 2023-06-28 10:06:56 浏览: 171
IDEA groovy生成实体解决乱码问题
Groovy闭包可以返回多个值,可以通过使用元组(Tuple)或者Map对象来实现。下面是两个返回值的示例:
1. 使用元组(Tuple)返回两个值
```groovy
def myClosure = { a, b -> return [a + b, a - b] }
def result = myClosure(5, 3)
assert result instanceof Tuple
assert result.size() == 2
assert result[0] == 8
assert result[1] == 2
```
2. 使用Map对象返回两个值
```groovy
def myClosure = { a, b -> return [sum: a + b, diff: a - b] }
def result = myClosure(5, 3)
assert result instanceof Map
assert result.size() == 2
assert result.sum == 8
assert result.diff == 2
```
阅读全文