Groovy tip 32 method parameter 1

Source: Internet
Author: User

Groovy tip 32 method parameter 1

 

 

The Groovy language has done a lot of useful work in the input parameters of methods. Some of these work can improve the readability of the code, and some can provide convenient and agile coding. In the previous text, we have introduced some of them one after another, such as map and DSL in groovy exploration. In this series, we will introduce some of the ones that are not mentioned above.

First, it is about the use of brackets when calling methods.

In the coding of groovy, we may be very skilled in using the following statement:

 

Println'Hello, world'

We all know that the preceding statement is equivalent to the following statement:

 

Println('Hello, World ')

This means that in groovy, We can omit the brackets when calling a method. The first advantage of this is to reduce the number of keyboard strokes in the brackets, it improves the speed of coding. The second is to improve the readability of the Code, which is also an aspect of DSL. For example, the following statement:

 

DefWORD = 'Are you OK? '

PrintlnWord

Just like"Println(Word) "good readability.

However, it is worth noting that the brackets in the method call cannot be omitted at will. In some cases, omission may cause compilation errors. The following line feed statement:

 

Println()

The parentheses cannot be omitted and are written as follows:

 

Println

In addition, the following statements cannot be compiled properly:

DefList = [1, 2, 4]

 

DefList2 = List. sublist 0, 1

However, the following statements can be compiled:

DefList1 = List. sublist (0, 1)

List. sublist 0, 1

 

In short, when you find that the brackets in the method call are omitted and the compilation error occurs, you should add the brackets.

 

2. Processing of the default values of method parameters in groovy.

For example, we have the following method:

 

Def StaticAdd (I, j)

{

I + J

}

 

In this method, if we only enter the parameter "I", the parameter "J" is the default value of 1. If we only enter the parameter "J", the default value is 0. If neither of the two parameters is input, "I" is the default value 0, and "J" is the default value 1.

To meet the above requirements, we must write the following three methods in Java:

 

Def StaticAdd0 (j)

{

Add (0, J)

}

Def StaticAdd1 (I)

{

Add (I, 1)

}

Def StaticAdd ()

{

Add (0, 1)

}

 

In this way, if you want to use the default value of the parameter "I", call the method "add0". If you want to use the default value of the parameter "J", call the method "Add1 "; if you want to call two default values, call the last method.

Therefore, in Java, to meet the above requirements, we must write four methods.

 

In groovy, we don't have to worry about it. In the groovy language, we introduce the default value of parameters, so that we can implement the above methods by only one method. As follows:

Def StaticAdd (I = 0, j = 1)

{

I + J

}

 

 

With the above method, we can make the following calls:

PrintlnAdd ()

PrintlnAdd (1)

PrintlnAdd (1, 2)

 

If no parameter is input for the first method call, the default values of the two parameters are used. If the second method call has only one input parameter, the default value of the parameter "J" is used; if the third method call has two input parameters, the default value of any parameter is not applicable.

The running result of the above Code is:

1

2

3

 

The only pity is that the parameter "I" is not the default value, and the parameter "J" is not the default value.

 

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.