Groovy exploration-adding constructors and static methods during the mop six Runtime

Source: Internet
Author: User

Groovy exploration-adding constructors and static methods during the mop six Runtime

 

 

 

 

Constructor is a method that we like to overload, because we encounter various situations when instantiating a class, such as in some situations, instances of a series of classes may have attributes with the same value. At this time, when instantiating an object, we do not want to inject these identical values into each object separately, this is a tedious task.

At this time, we will reload the constructor, but sometimes, for example, some bean objects have many attributes, so we cannot load many constructor in the class. For example, we have the following yybean class:

 

ClassReader {

String Province

String City

String name

IntAge

 

}

 

This is a very simple yybean class, and we will not reload the constructor in it. We generally perform the following instantiation:

 

DefReader =NewReader (Province: 'guangdong', city: 'shenzhen ', name: 'Tom', age: 22)

 

Now, we have a scenario where a group of readers may come from Shenzhen, Guangdong province, so it is too tedious to initialize every reader.

At this time, we can reload a constructor for the policybean class above during the runtime. As follows:

 

Reader. metaclass. constructor = {string name,IntAge->

NewReader (Province: 'guangdong', city: 'shenzhen ', name: name, age: age)

}

 

In this constructor, we assign the properties "Province" and "city" to the default values, and then inject two parameters to the constructor. The test code is as follows:

 

DefReader1 =NewReader ('Tom ', 22)

Println"""

Name :$ {reader1.name}

Age: $ {reader1.age}

Address: $ {reader1.province }$ {reader1.city }"""

DefReader2 =NewReader ('Mike ', 20)

Println"""

Name :$ {reader2.name}

Age: $ {reader2.age}

Address: $ {reader2.province }$ {reader2.city }"""

 

The running result is:

 

Name: Tom

Age: 22

Address: Guangdong Shenzhen

 

Name: Mike

Age: 20

Address: Guangdong Shenzhen

 

 

We can also add some static methods to the class during runtime. There are two ways to add static methods here. The first is to intercept the class during runtime. For example, we have the following class:

 

ClassFoo {

}

 

Currently there is no method in this class. We will add a static method to it during the runtime, as shown below:

 

Foo. metaclass. 'static '. invokemethod = {

String name, args1->

DefMetamethod = Foo. metaclass. getstaticmetamethod (name, args1)

DefResult

If(Metamethod) Result = metamethod. Invoke (name, args1)

ElseResult = 'foo'

Result

}

 

Then we can test it:

 

PrintlnFoo. Foo ()

The running result is:

Foo

 

It is worth noting that the following code lines:

DefMetamethod = Foo. metaclass. getstaticmetamethod (name, args1)

 

Is used to obtain the specified static method. If it exists, it is called directly; otherwise, "foo" is returned ".

If you add the following static method to the above Foo class:

 

Def StaticBar ()

{

'Bar'

}

 

Perform the following tests:

PrintlnFoo. Foo ()

PrintlnFoo. Bar ()

 

The result is:

Foo

Bar

 

 

This interception method is actually to load the static "invokemethod" method during the runtime. In addition, we can directly add static methods.

Or the above Foo class, we can use the following method to add static methods:

 

 

Foo. metaclass. 'static '. Hello = {

Args1->

Return"Hello, ${args1 }"

}

 

 

This method is very easy to add. if you remove the "static", it will become a common method.

Now let's perform a test:

 

PrintlnFoo. Hello ('World ')

 

The running result is:

Hello, world

 

 

 

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.