1
比方說,我有這樣的事情:我可以在Groovy中傳入方法來代替封閉嗎?
class Test {
def test_method() {
def http = new HTTPBuilder("http://rest.request.com")
http.request(groovyx.net.http.Method.GET) { req ->
uri.path = "/path/to/rest/request"
response.success = {resp, reader ->
println resp
}
}
}
}
這工作得很好,所有的,但我真的很喜歡做這樣的事情:
class Test {
def print_resp(String resp) {
println resp
}
def test_method() {
def http = new HTTPBuilder("http://rest.request.com")
http.request(groovyx.net.http.Method.GET) { req ->
uri.path = "/path/to/rest/request"
response.success = print_resp
}
}
}
除了我已經得到了語法錯了。請幫我理解我做錯了什麼。
互聯網何時中斷? http://mrhaki.blogspot.com/2009/08/groovy-goodness-turn-methods-into.html – user2864740 2014-09-27 23:08:31