Groovy Quest Mop Eight add properties or methods to classes and objects during the runtime

Source: Internet
Author: User

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()

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.