The mop of groovy exploration add constructors and static methods during the runtime

Source: Internet
Author: User

Constructors are one way we like to overload, because we encounter a variety of situations when instantiating a class, such as in some cases, instances of a series of classes might have properties of the same value, then when we instantiate the object, we don't want to inject the same values into each object individually, This kind of work is very complicated.

At this point, we overload the constructor, but some times, such as some bean objects, their properties are many, we are not good at overloading many constructors in the class. For example, we have one of the following Groovybean classes:

class Reader {
String province
String city
String name
int age

}

This is a very simple Groovybean class, we will not overload the constructor inside. Generally we will do the following instantiation:

def reader = new Reader(province:'Guangdong',city:'Shenzhen',name:'Tom',age:22)

Now, we have this scenario: there may be a group of readers, all from Guangdong province Shenzhen, then we have every reader to do the above initialization, it seems too cumbersome.

At this point, we can overload a constructor for the Groovybean class above during the runtime. As shown below:

Reader.metaClass.constructor = {String name,int age ->
new Reader(province:'Guangdong',city:'Shenzhen',name:name,age:age)
}

In this constructor, we assign the attribute "province" and "city" to the default value, and then inject two parameters into the constructor. The test code is as follows:

def reader1 = new Reader('Tom',22)
println"""
name:    ${reader1.name}
age:     ${reader1.age}
address: ${reader1.province} ${reader1.city}"""
def reader2 = new Reader('Mike',20)
println """
name:    ${reader2.name}
age:     ${reader2.age}
address: ${reader2.province} ${reader2.city}"""

The results of the operation are:

name:    Tom
age:     22
address: Guangdong Shenzhen

name:    Mike
age:     20
address: Guangdong Shenzhen

We can also add static methods to the class during the runtime, there are two ways to add static methods, the first of which is to intercept during the runtime. For example, we have one of the following classes:

class Foo {
}

There's not a single method in this class now, and we're going to add a static method to it during the run time, as follows:

Foo.metaClass.'static'.invokeMethod = {
String name,args1 ->
def metaMethod = Foo.metaClass.getStaticMetaMethod(name,args1)
def result
if(metaMethod) result = metaMethod.invoke(name,args1)
else result = 'foo'
result
}

So, we can test it:

println Foo.foo()

The results of the operation are:

foo

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.