2015-01-09 56 views
0

the documentation, 13.5.5把封閉

當一個方法的最後一個參數是一個閉包,你可以在方法調用

好後放置 關閉,我試過了,但它沒我確實按我的設想工作。請看下面的代碼:

def repostiory_closure = { 
    mavenCentral() 
} 

repositories{ //OK 
    mavenCentral() 
} 

repositories(){ //OK 
    mavenCentral() 
} 

repositories repostiory_closure //OK 
repositories() repostiory_closure //compile-time error 

所以我們可以把只有封閉文字一個方法調用之後,但Closure類型的變量。是對的嗎?

回答

2

你需要做的就是把封口的方法調用的參數,像這樣:

repositories(repostiory_closure)

如此,因爲最後一個參數是一個封閉的方法可以如上內嵌調用。