What is the use of Meta-Class in Groovy and other OO programming languages?
Java has no meta-class. You are probably thinking about this:
A metaclass within Groovy defines the behavior of any given groovy or java class. The MetaCLASS interface defines two parts. Client API, defined by contract with MetaAbject Protocol extension and Groove Runtime system. Generally the compiler and the Groove Runtime engine interact with the methods of this class, while MetaClass clients interact with the method defined by the MetaObjectProtocol interface
Groovy MetaClass assigns you behavior and state Allows classes on runtime without editing the original source code, it is a layer above the original class.
This is a mechanism used by Groove.
Example:
Object.class.metaClass.explode {-> Println "Boom! $ {Representative} Explode!"} "Some String". Explode (); 12345.explode ();
Output:
Boom! Some String Explosions!
Boom! 12345 explosion!For more advanced use, read:
Comments
Post a Comment