在Groovy中,可以通过使用集合或者自定义对象作为返回值来返回多个参数。
如果要返回多个参数且参数类型相同的情况下,可以使用集合类来存储这些参数,如List或者Map。下面是一些示例:
List<Integer> getNumbers() {
def num1 = 10
def num2 = 20
return [num1, num2]
}
def result = getNumbers()
def num1 = result[0]
def num2 = result[1]
println "num1: $num1, num2: $num2"
推荐的腾讯云相关产品:云服务器(CVM),具体产品介绍可查看链接地址:https://cloud.tencent.com/product/cvm
Map<String, String> getUserInfo() {
def name = "John Doe"
def age = "25"
return [name: name, age: age]
}
def result = getUserInfo()
def name = result.name
def age = result.age
println "name: $name, age: $age"
推荐的腾讯云相关产品:云数据库 MySQL版(CDB),具体产品介绍可查看链接地址:https://cloud.tencent.com/product/cdb
如果要返回多个参数且参数类型不同,可以创建一个自定义对象来存储这些参数。下面是一个示例:
class Person {
String name
int age
Person(String name, int age) {
this.name = name
this.age = age
}
}
Person getPersonInfo() {
def name = "John Doe"
def age = 25
return new Person(name, age)
}
def result = getPersonInfo()
def name = result.name
def age = result.age
println "name: $name, age: $age"
推荐的腾讯云相关产品:云函数(SCF),具体产品介绍可查看链接地址:https://cloud.tencent.com/product/scf
领取专属 10元无门槛券
手把手带您无忧上云