Parameter two of the Groovy Tip 33 method

Source: Internet
Author: User
Tags integer

The map parameter is one of the most commonly used parameter types in the code of the Groovy language, and we often write the following code naturally:

t.testMap(a:1)
t.testMap(a:1,b:1)
t.testMap(a:1,b:1,c:1)
new Person(name:'Tom',sex:23)

These are the uses of the map parameter. And so on, we also often want the list parameter to have the same function, for example, we have the following method with the list object as a parameter:

class Testor2 {
    def testList(List list)
    {
       println list
    }
}

We then wrote the following tests:

def t = new Testor2()
      t.testList(1,2,3)

We are eager to expect such code to work, unfortunately, the above code will be reported as the following error:

Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: str.Testor2.testList() is applicable for argument types: (java.lang.Integer, java.lang.Integer, java.lang.Integer) values: {1, 2, 3}
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap (ScriptBytecodeAdapter.java:54)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap (ScriptBytecodeAdapter.java:59)

But don't be discouraged, in fact, our array parameters have such a function. For example, we have one of the following methods that takes an array as a parameter:

class Testor2 {

    def testArray(int[] arrs)
    {
       println arrs
    }
}

In this way, we can write the following tests:

def t = new Testor2()
      t.testArray(1,2,3)

The results of the operation are:

[1, 2, 3]

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.