前言基本使用带参数的方法有返回值的方法参数是map的方法
前言
本篇学习在groovy中如何定义和使用方法 基本使用
// 定义方法
def fun_test() {
println('这是一个方法!')
}
// 调用方法
fun_test()
带参数的方法
// 带参数的方法
def fun2_test(int a, int b) {
println("sum is:" + (a + b))
}
// 调用方法时,需传入参数
fun2_test(1, 2)
有返回值的方法
// 有返回值的方法 static 关键字 静态方法
static def fun3_test(int c, int d) {
return (c + d)
}
// sum 接收返回值
sum = fun3_test(2, 3)
// 打印返回值
println(sum)
参数是map的方法
// 参数是map的方法
class MethodDemo {
static void main(args) {
def name = "大海"
def age = 28
def gender = "男"
def city = "Beijing"
printPersonInfo(name:name, age:age, gender:gender, city:city)
}
static def printPersonInfo(Map args) {
def name = args.name
def age = args.age
def gender = args.gender
def city = args.city
def personInfo = """
${name} is from ${city},
he is ${age} old, his gender is ${gender}
"""
println personInfo
}
}



