我可以在Groovy中传递一个方法来代替闭包吗?
假设我有这样的东西:我可以在Groovy中传递一个方法来代替闭包吗?,groovy,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 =
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
}
}
}
这一切都很好,但我真的更喜欢这样做:
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
}
}
}
不过我把语法搞错了。请帮助我理解我做错了什么。是否要使用语法:
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 = this.&print_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 = this.&print_resp
}
}
互联网什么时候断了?互联网什么时候断了?