美文网首页
grovvy(7)-闭包

grovvy(7)-闭包

作者: 高斯巴 | 来源:发表于2018-11-12 14:50 被阅读0次

特点:

1:默认参数:it(不传参数时,会有默认参数it.有参数时则没有隐式参数it)

def clouser3={println("hello groovy${it}")}

clouser3(name)//hello groovy3333

2:grovvy中闭包如果是最后一个参数可以不放在括号内,可以直接放在括号外

number.downto(1){    num->result *=num}

3:如果参数只有一个闭包,小括号也可以省略

number.downsd{

    num->result *=num

}

package variable

//闭包概念:代码块

//闭包的定义

def clouser={

    println('hello groovy')

}

//闭包的调用

clouser.call()//使用call 调用

clouser()//想方法一样调用

//增加参数

def clouser1={String name ->println("hello groovy${name}")}

clouser1.call("222")//hello groovy222

clouser1("222")//hello groovy222

def name="3333"

clouser1(name)//hello groovy3333

def clouser2={String arg1,int age->println("hello groovy${name},age${age}")}

clouser2('lich',233)//hello groovy3333::age233

//默认参数:it(不传参数时,会有默认参数it.有参数时则没有隐式参数it)

def clouser3={println("hello groovy${it}")}

clouser3(name)//hello groovy3333

//返回值,闭包是一定有返回值的

def clouser4={

    println('hello groovy')

}

def result=clouser4()

    println(result)////null .无定义return时,返回值为null

def clouser5={String aa->

    return"hello ${aa}";

}

def result2=clouser5("hahaah")

println(result2)//hello hahaah

相关文章

  • grovvy(7)-闭包

    特点: 1:默认参数:it(不传参数时,会有默认参数it.有参数时则没有隐式参数it) def clouser3=...

  • idea+gradle学习笔记

    1.idea grovvy tool 基本语法 闭包 3.gradle配置文件介绍build.gradle 4.g...

  • Swift5.1闭包

    7.闭包 闭包表达式闭包表达式一般形式:{ (参数列表) -> 返回值类型 in statements...

  • swift-闭包

    闭包 闭包定义 闭包简化 - 尾随闭包 闭包参数 闭包返回值 闭包的循环引用

  • 7_闭包

    Swift 的闭包表达式拥有简洁的风格,并鼓励在常见场景中进行语法优化,主要优化如下: 利用上下文推断参数和返回值...

  • #7 swift 闭包

    swift 中的闭包和JS中的匿名函数很像,但是它还有一些其它的特性,其中包括一些看起来很怪异的语法糖,写法看起来...

  • 7.闭包

    sort 函数 func backward(a:Int, b:Int) -> Bool { return a > ...

  • Swift闭包_7

    闭包是自包含的函数代码块,可以在代码中被传递和使用,swift中闭包与c和oc的代码块匿名函数比较相似 闭包可以捕...

  • Swift——7、闭包

    闭包 在Swift中,可以通过func定义一个函数,也可以通过闭包表达式定义一个函数 闭包表达式 两者的不同,函数...

  • Swift闭包(7)

    教程目录与OC一样的内容不重复介绍 闭包 闭包其实和OC中的Block差不多,作用是一样的.下面说一下主要的用法和...

网友评论

      本文标题:grovvy(7)-闭包

      本文链接:https://www.haomeiwen.com/subject/ggccfqtx.html