Groovy explores the deep use of the AS keyword

Source: Internet
Author: User

In the previous series, "Groovy Tip" as "keyword usage", we've talked about some of the exciting uses of the "as" keyword. This article goes on to continue to show some of the more exciting uses of the "as" keyword in the previous section.

In the previous article, we talked about the use of the "as" keyword as a type conversion. The most common uses are the following:

def list = ['a','b','c']
println list.class
def strs = list as String[]
println strs.class

This is a commonly used type conversion code, from the following print results can be seen, the "as" keyword use does make the variable type conversion.

class java.util.ArrayList
class [Ljava.lang.String;

Seeing this usage, we may wonder how this usage of the "as" keyword is implemented. How do we want to transform each of the two classes that we have designed?

For example, we have the following two classes:

class A
{
def x

}

class B
{
def x
}

If we've got an object of Class A, as follows:

def a = new A(x:3)

So how do we get to a Class B object with the following statement?

def b = a as B

In fact, quite simply, we just need to implement the "Astype" method in Class A, as follows:

class A
{
def x

Object asType(Class type)
{
if(type == B) return new B(x:this.x+1)
}
}

This is the class A that we've rewritten, so we can now test to see if we've achieved the desired functionality:

def a = new A(x:3)
def b = a as B
println b.class.simpleName
println b.x

The results of the operation are:

B
4

As you can see, it is true that we have achieved the functionality we envisioned.

In addition, the "as" keyword has made us surprised to find. For example, we have a simple class like the following:

class C
{
def x
def y

public C(x,y)
{
this.x = x
this.y = y
}
}

So, we can instantiate an instance of it like this:

def list = [1,2]
def c = list as C
println c.class.simpleName
println "${c.x},${c.y}"

The results of the run are:

C
1,2

It's just like magic.

Wait a minute, and then look at the following example, for example, we define an interface as follows:

interface D
{
def test_d()
}

We can use the "as" keyword like this:

def map = [test_d:{println 'ok'}]
def d = map as D
println d.class.simpleName
d.test_d()

The results of the operation are:

$Proxy0
ok

It's really cool! As we can see, the "as" keyword is a very important keyword in the groovy language, and its usage embodies the features of the groovy language as a dynamic language, and everything can be decided during the runtime, fully demonstrating the flexible, fast features of groovy's language.

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.