As we all know, in the groovy language, we can use the mop attribute to add properties or methods to the runtime.
This addition includes two levels of additions:
The first is to add a property or method to a class. That is, if we add a property or method to a class during the runtime, then all the objects instantiated by that class will have this property or method added later.
Second, the second is to add a property or method to an object. That is, if we add a property or method to an object during the run time, then only the object will have the property or method after it has been added. In other words, if we instantiate an object to the class of this object, then the object cannot have the property or method that we just added.
Is that really the case? Let's give a few examples to see.
Using Expondometaclass to add properties or methods to a class during the run time is one of our most common ways to add properties or methods to a runtime. For example, we have one of the following classes:
class Testor1 {
}
Now we're trying to use Expondometaclass to add a method to Testor1 during the run time and test it:
def t = new Testor1()
try
{
println 't invoke far'
t.far()
}
catch(Exception e)
{
}
Testor1.metaClass."far" = {
->
println 'far'
}
def t1 = new Testor1()
println 't1 invoke far'
t1.far()
def t2 = new Testor1()
println 't2 invoke far'
t2.far()
The test is simple, we instantiate a Testor1 object before adding the method, then instantiate the two Testor1 objects after adding the method, and test them separately.
The results are:
t invoke far
t1 invoke far
far
t2 invoke far
far
As you can see from the results: the object instantiated before adding a method cannot invoke the "far" method, and the two objects instantiated after adding the method can call the "far" method.
This is the result of adding a method or property to a class during the run time.
In fact, there is a way to add a property or method to a class during the runtime using the mop feature, which is the following method:
def t = new Testor1()
try
{
println 't invoke far'
t.far()
}
catch(Exception e)
{
}
def mc = new ExpandoMetaClass(Testor1.class,true)
mc.far = {
->
println 'far'
}
mc.initialize()
def t1 = new Testor1()
println 't1 invoke far'
t1.far()
def t2 = new Testor1()
println 't2 invoke far'
t2.far()
}