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

如何接受多个参数从返回函数在groovy

发布时间:2020-12-14 16:39:55 所属栏目:大数据 来源:网络整理
导读:我想从groovy中写的函数返回多个值并接收它们,但我收到一个错误 class org.codehaus.groovy.ast.expr.ListExpression,with its value ‘[a, b]’,is a bad expression as the left hand side of an assignment operator 我的代码是 int a=10int b=0println
我想从groovy中写的函数返回多个值并接收它们,但我收到一个错误

class org.codehaus.groovy.ast.expr.ListExpression,with its value ‘[a,
b]’,is a bad expression as the left hand side of an assignment
operator

我的代码是

int a=10
int b=0
println "a is ${a},b is ${b}"
[a,b]=f1(a)
println "a is NOW ${a},b is NOW ${b}"

def f1(int x) {   
  return [a*10,a*20]
}

解决方法

你几乎有了。 [a,b]创建一个列表,(a,b)展开一个列表,所以你想要(a,b)= f1(a)而不是[a,b] = f1

int a=10
int b=0
println "a is ${a},b is ${b}"
(a,b)=f1(a)
println "a is NOW ${a},b is NOW ${b}"

def f1(int x) {
    return [x*10,x*20]
}

(编辑:李大同)

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

    推荐文章
      热点阅读