Groovy Explore MOP two various operations on class attributes

Source: Internet
Author: User

We talked about mop, that is, "Meta Object Protocol", is the class or object elements, such as names, methods, attributes, and so on, in the run-time of real-time changes, such as the modification method name, attribute name, dynamic increase methods, attributes and so on a class of programming collectively.

For example, we have talked about the "InvokeMethod" and "methodmissing" two methods, can be used to enable us to dynamically in the runtime to add methods to a class, it is noteworthy that this increase is class-level, that is, all objects of a class can add methods.

Now that you've said it's time to increase the dynamic of a class in the runtime, the corresponding addition to the dynamic of a class at run time is what we're going to consider.

As we all know, in the Java language, operations on properties are performed through the "Get" and "set" Methods of the attribute, whereas in the groovy language the operation of the property is done through Gpath, which allows us to program the Groovybean class Either writing the Groovybean class or accessing the properties of the Groovybean object is much simpler than the JavaBean in the Java language.

For example, we have one of the following groovybean:

class Employee
{
String id

String name
}

We will now initialize it:

def empl1 = new Employee(id:'00001',name:'Tom')

Now we can access its properties through Gpath:

println empl1.name

Of course, the so-called Gpath access, that is, through "." operator access, such as "Empl1.name", does not directly access the "name" attribute of the "Empl1" object, but is accessed through the "Get" and "set" methods implemented implicitly by the "Employee" class. Having understood this, we are free to add attributes to the Groovybean class or object. Take a look at the following example:

class Employee
{
String id

String name

def getAge()
{
'everyone is 24'
}
}

As you can see, we did not define the "age" attribute for the "Employee" class, but we implemented the "Getage" method, and we can access the "age" attribute through Gpath, see the following code example:

def empl1 = new Employee(id:'00001',name:'Tom')


println empl1.name

println empl1.age

The results of the operation are:

Tom

Everyone is 24

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.