You must be familiar with the "invokemethod" method. We can use the following simple example to see its role:
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)
}
}
The running result is:
Invoke Hello directly
Unknown method Foo (mark, 19)
It can be seen that for method calls of an object, if this method can be assigned, the above "hello" method can be found in the invoketestor1 class, it is assigned to the "Hello" method of the invoketestor1 class. If it cannot be assigned, the "Foo" method above will be called.
In the groovy language, there is another way to implement the above functions. This is the "methodmissing" method. Please refer to the following example:
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)
}
}
Let's take a look at the aboveCodeRunning result:
Invoke Hello directly
Unknown method Foo (mark, 19)
We can see that the "methodmissing" method is the same as its name. If the method can be found in the class, this method is called. If it cannot be found, it is "missing method ", you can call the "methodmissing" method. Similar to the "invokemethod" function.
Everyone knows this, but in fact, "invokemethod" is used to assign all methods of an object in groovy. To do this, you need to use the groovyinterceptable interface. See the following example:
Class invoketestor2 implements policyinterceptable {
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)
}
}
The running result is:
Unknown method Hello ()
Unknown method Foo (mark, 19)
From the running results, we can see that the "invokemethod" method can indeed assign all methods, as long as we implement the "yyinterceptable" interface.
However, even if the class implements the groovyinterceptable interface, it cannot use the "methodmissing" method to dispatch all methods. See the following example:
Class methodtestor2 implements policyinterceptable {
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)
}
}
Its running result is:
Invoke Hello directly
Unknown method Foo (mark, 19)
Through the comparison above, we can see the subtle difference between the "invokemethod" method and the "methodmissing" method: that is, the "invokemethod" method can assign all methods, includes the implemented and unimplemented methods of a class, and it implements the above functions through this class to implement the "yyinterceptable" interface. The "methodmissing" method can only assign methods that are not implemented by a class, whether or not it implements the "yyinterceptable" interface.
This difference is indeed very subtle. If we want a method to manage the calls of all methods of a class, we must use the "invokemethod" method; if we only want to use one method to manage all the "missing methods" of a class, that is, the "methodmissing" method is more effective. Of course, the "invokemethod" method can also implement the "methodmissing" method.