對于“invokeMethod”方法,大家一定很熟悉了,我們可以用下面一個簡單的例子來看看它的作用:
class InvokeTestor1 {
def hello()
{
'invoke hello directly'
}
def invokeMethod(String name,Object args)
{
return "unknown method $name(${args.join(',')})"
}
static void main(args) {
def it = new InvokeTestor1()
println it.hello()
println it.foo("mark",19)
}
}
運(yùn)行的結(jié)果為:
invoke hello directly
unknown method foo(mark,19)
可以看出,對于一個對象的方法調(diào)用來說,如果這個方法能夠被分派出去,如上面的“hello”方法,可以在InvokeTestor1類中找到,就被分派給InvokeTestor1類的“hello”方法;如果不能被分派,如上面的“foo”方法,則調(diào)用“invokeMethod”方法。
在Groovy語言中,還有一個方法也可以實(shí)現(xiàn)上面的功能,這就是“methodMissing”方法,請看下面的例子:
class MethodTestor1 {
def hello()
{ "invoke hello directly"
}
def methodMissing(String name,args)
{
return "unknown method $name(${args.join(',')})"
}
static void main(args) {
def mt = new MethodTestor1()
println mt.hello()
println mt.foo('mark',19)
}
}
我們還是來看看上面的代碼的運(yùn)行結(jié)果:
invoke hello directly
unknown method foo(mark,19)
Examda提示: 可以看到,“methodMissing”方法就像它的名字一樣,如果方法可以在類中找得到,那么就調(diào)用該方法;如果找不到,那么就是“missing method”,就可以調(diào)用“methodMissing”方法了。跟“invokeMethod”功能非常類似。
這點(diǎn)大家都清楚,但實(shí)際上,“invokeMethod”在Groovy語言中是用來分派一個對象的所有方法的。要做到這一點(diǎn),就需要借助于“GroovyInterceptable”接口。請看下面的例子:
class InvokeTestor2 implements GroovyInterceptable{
def hello()
{
"invoke hello directly"
}
def invokeMethod(String name,Object args)
{
return "unknown method $name(${args.join(',')})"
}
static void main(args) {
def it = new InvokeTestor2()
println it.hello()
println it.foo('mark',19)
}
}
考試大運(yùn)行結(jié)果為:
unknown method hello()
unknown method foo(mark,19)
從運(yùn)行結(jié)果可以看出,“invokeMethod”方法的確可以分派所有的方法,只要我們實(shí)現(xiàn)“GroovyInterceptable”接口即可。
而“methodMissing”方法呢,即使類實(shí)現(xiàn)了“GroovyInterceptable”接口,它也不能使用“methodMissing”方法來分派所有的方法。請看下面的例子:
class MethodTestor2 implements GroovyInterceptable{
def hello()
{
"invoke hello directly"
}
def methodMissing(String name,args)
{
return "unknown method $name(${args.join(',')})"
}
static void main(args) {
def mt = new MethodTestor2()
println mt.hello()
println mt.foo('mark',19)
}
}
Examda提示它的運(yùn)行結(jié)果為:
invoke hello directly
unknown method foo(mark,19)
通過了上面的比較,我們可以看出“invokeMethod”方法和“methodMissing”方法的微妙區(qū)別:即,“invokeMethod”方法可以分派所有的方法,包括一個類已經(jīng)實(shí)現(xiàn)了的和未實(shí)現(xiàn)的方法;而它實(shí)現(xiàn)上面的功能是通過這個類實(shí)現(xiàn)“GroovyInterceptable”接口達(dá)到的。而“methodMissing”方法則只能分派一個類未實(shí)現(xiàn)的方法,無論它是否實(shí)現(xiàn)了“GroovyInterceptable”接口。
這種區(qū)別的確很微妙,如果我們想讓一個方法來管理一個類所有方法的調(diào)用,那么我們必須使用“invokeMethod”方法;如果我們只想通過一個方法來管理一個類的所有“missing method”,即不能被分派出去的方法,那么使用“methodMissing”方法是比較有效的;當(dāng)然,“invokeMethod”方法也能實(shí)現(xiàn)“methodMissing”方法的功能
class InvokeTestor1 {
def hello()
{
'invoke hello directly'
}
def invokeMethod(String name,Object args)
{
return "unknown method $name(${args.join(',')})"
}
static void main(args) {
def it = new InvokeTestor1()
println it.hello()
println it.foo("mark",19)
}
}
運(yùn)行的結(jié)果為:
invoke hello directly
unknown method foo(mark,19)
可以看出,對于一個對象的方法調(diào)用來說,如果這個方法能夠被分派出去,如上面的“hello”方法,可以在InvokeTestor1類中找到,就被分派給InvokeTestor1類的“hello”方法;如果不能被分派,如上面的“foo”方法,則調(diào)用“invokeMethod”方法。
在Groovy語言中,還有一個方法也可以實(shí)現(xiàn)上面的功能,這就是“methodMissing”方法,請看下面的例子:
class MethodTestor1 {
def hello()
{ "invoke hello directly"
}
def methodMissing(String name,args)
{
return "unknown method $name(${args.join(',')})"
}
static void main(args) {
def mt = new MethodTestor1()
println mt.hello()
println mt.foo('mark',19)
}
}
我們還是來看看上面的代碼的運(yùn)行結(jié)果:
invoke hello directly
unknown method foo(mark,19)
Examda提示: 可以看到,“methodMissing”方法就像它的名字一樣,如果方法可以在類中找得到,那么就調(diào)用該方法;如果找不到,那么就是“missing method”,就可以調(diào)用“methodMissing”方法了。跟“invokeMethod”功能非常類似。
這點(diǎn)大家都清楚,但實(shí)際上,“invokeMethod”在Groovy語言中是用來分派一個對象的所有方法的。要做到這一點(diǎn),就需要借助于“GroovyInterceptable”接口。請看下面的例子:
class InvokeTestor2 implements GroovyInterceptable{
def hello()
{
"invoke hello directly"
}
def invokeMethod(String name,Object args)
{
return "unknown method $name(${args.join(',')})"
}
static void main(args) {
def it = new InvokeTestor2()
println it.hello()
println it.foo('mark',19)
}
}
考試大運(yùn)行結(jié)果為:
unknown method hello()
unknown method foo(mark,19)
從運(yùn)行結(jié)果可以看出,“invokeMethod”方法的確可以分派所有的方法,只要我們實(shí)現(xiàn)“GroovyInterceptable”接口即可。
而“methodMissing”方法呢,即使類實(shí)現(xiàn)了“GroovyInterceptable”接口,它也不能使用“methodMissing”方法來分派所有的方法。請看下面的例子:
class MethodTestor2 implements GroovyInterceptable{
def hello()
{
"invoke hello directly"
}
def methodMissing(String name,args)
{
return "unknown method $name(${args.join(',')})"
}
static void main(args) {
def mt = new MethodTestor2()
println mt.hello()
println mt.foo('mark',19)
}
}
Examda提示它的運(yùn)行結(jié)果為:
invoke hello directly
unknown method foo(mark,19)
通過了上面的比較,我們可以看出“invokeMethod”方法和“methodMissing”方法的微妙區(qū)別:即,“invokeMethod”方法可以分派所有的方法,包括一個類已經(jīng)實(shí)現(xiàn)了的和未實(shí)現(xiàn)的方法;而它實(shí)現(xiàn)上面的功能是通過這個類實(shí)現(xiàn)“GroovyInterceptable”接口達(dá)到的。而“methodMissing”方法則只能分派一個類未實(shí)現(xiàn)的方法,無論它是否實(shí)現(xiàn)了“GroovyInterceptable”接口。
這種區(qū)別的確很微妙,如果我們想讓一個方法來管理一個類所有方法的調(diào)用,那么我們必須使用“invokeMethod”方法;如果我們只想通過一個方法來管理一個類的所有“missing method”,即不能被分派出去的方法,那么使用“methodMissing”方法是比較有效的;當(dāng)然,“invokeMethod”方法也能實(shí)現(xiàn)“methodMissing”方法的功能