Grails Note four: Groovy feature summary

Source: Internet
Author: User
Tags grails

Working with Groovy while learning grails is unavoidable, though not too deep, but it's also helpful to know a few features.

1. Get integers after dividing

Use the intdiv () method to get an integer, note that this method only applies to dividing two integers , floating-point numbers do not

If,while acceptable type of 2.Groovy
    • Boolean
    • Number 0 is false, others are true
    • String null character "" is false, others are true
    • The object is null when False, others are true
    • Collection Set Property Length=0 false, others true
3. How closures are accessed

The closures and methods in groovy are similar in that they are called directly in two ways:

Define a closure def CLOS={PRINTLN it}//first closure call Clos.call (' Obama ')///second closure call Clos (' dumb pull-out light ')
How the key value of map in 4.Groovy is a variable

In groovy, the key value of map is a string by default, and if you want the key value to be a variable, enclose the variable in ():

def x= ' 123 ' Def map=[(x): 45]println map.tostring ()
Two different points of the 5.Groovy class and the Java class

1.Groovy class has a default constructor based on map initialization

Constructs a groovy class cs{    String name}//instantiation def C=new CS (name: ' Tita ')//or Def CC=NEW cs ([Name: ' TT '])

The attribute field of the 2.Groovy class does not have an access qualifier defined, and the default is private

Class a{string name}//equivalent class a1{    private string name public    string GetName () {        name    } public    void SetName (String name) {        this.name=name    }}def a=new A (name: ' TT ')//i.e. the following equivalent println a.nameprintln a.getname ()

The use of the * number operator for 6.List

The list has a very useful operator,--*, which is useful in two cases:

1. Place the * number behind a list object, such as

def list=[1,2,3]def Temp=list*.intdiv (2) println Temp

At this point, each element in the list object calls the method that follows the *, and the return is still a list object

2. Put in front of the range, such as:

 

def range=0..9def Temp=[*range]println Temp

Note [] is necessary because this way is to expand the scope to generate a list object

Grails Note four: Groovy feature summary

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.