def PRintWithOneParameter(block) { block("Michael")}printWithOneParameter { println it }printWithOneParameter { name -> println name }返回MichaelMichael2、多个参数时,需要逐一指定。def printWithTwoParameter(block) { block "Michael", "Jordan"}printWithTwoParameter {firstName, secondName -> println "$secondName, $firstName $secondName"}返回Jordan, Michael Jordan3、可以预先绑定相应的参数。def printWithTwoParameter(block) { newBlock = block.curry("Michael") newBlock "Jordan" newBlock "Jackson"}printWithTwoParameter {firstName, secondName -> println "$secondName, $firstName $secondName"}返回Jordan, Michael JordanJackson, Michael Jackson4、类似的方法还有rcurry和ncurry。def printWithTwoParameter(block) { newBlock = block.rcurry("Michael") newBlock "Jordan" newBlock "Jackson"}printWithTwoParameter {firstName, secondName -> println "$secondName, $firstName $secondName"}返回Michael, Jordan MichaelMichael, Jackson Michael5、ncurry调用格式是ncurry(index, block)6、maximumNumberOfParameters属性,闭包参数的数量。def printWithTwoParameter(block) { println block.maximumNumberOfParameters}printWithTwoParameter {firstName, secondName -> println "$secondName, $firstName $secondName"}返回2
新闻热点
疑难解答