def LIMIT=10
def count=1
println 'start'
while(count<=LIMIT){
println "count:${count}"
count++
}
println 'Done'

Be careful : Fixed variables are written in uppercase

for sentence :

def LIMIT =10;
println 'start'
for (count in 1..LIMIT){
println "${count}"
}
println 'done'

Loop processing list :

 println 'start'
for (count in [11,12,13,14]){
println "${count}"
}
println 'done'

Loop processing mapping :

 def staff=['ken':21,'john':25,'sally':22]
def totalAge=0;
for(staffEntry in staff){
totalAge+=staffEntry.value
} print "total age is ${totalAge}"

Loop through strings :

 def name='Kenneth'
def listofChar=[]
for(letter in name){
listofChar << letter
}
println "listofChar:${listofChar}"

if Judge :

 def first=12
def second=34
if(first>second){
println "${first}>${second}"
}else{
println "${second}>${first}"
}

switch:

 
 
def n=10
switch (n){
case 1:println 'one'
case 2:println 'two'
case 3:println 'three'
case 10:println 'ten'
}
 
 

switch And scope :

def score=89
switch (score){
case 70..100:
grade='A'
break
case 60..70:
grade='b'
break
case 50..60:
grade='C'
break
case 40..50:
grade='E'
break
}
println "${score}:${grade}"

List and case:

def number=89
switch (number){
case [86,87,88,89]:
println 'number is eighty something'
break
case [66,67,68,69]:
println 'number is sixty something' break }
 Closures and their calls :
def clos={println 'Hello world'}
clos.call()
 
 

Parameterized closure :

def clos={param->println "Hello ${param}"}
clos.call('Andy')

Single hidden parameter :

def clos={println "Hello ${it}"}
clos.call('Andy')

Among them ${it} Medium it representative iterator

each Methods and closures :
[1,2,3,4].each {println it}
['ken':21,'jphn':22,'sally':45].each {println it}
['ken':21,'joph':22].each {println "${it.key} maps to :${it.value}"}
 
 

Conditional elements :

[1,2,3,4].each {num->if(num%2==0) println num}
['ken':21,'jphn':22,'sally':45].each {staff->if(staff.value>=25)println staff.key}

any and every:

def anyElement=[11,12,13,14].any{element->element>12}
println "${anyElement}" def someElement=[11,12,13,14].every{element->element>12}
println "${someElement}"

collect Method :

 
 
def list=[1,2,3,4].collect{element->return element*element}
println "list:${list}"
 
 

collect Advanced applications :

def doubles={item->2*item}
def triples={item->3*item}
def map(clos,list){
return list.collect(clos)
}
println "Doubleing:${map(doubles,[1,2,3,4])}"
println "Tripling:${map(triples,[1,2,3,4])}"

Grovvy class :

class Account1 {
def number
def balance
}
def acc=new Account1(number:'ACB123',balance:1200)
println "Account ${acc.number} has balance ${acc.balance}"

Be careful :acc.number I used to visit Account Object's acc Of number attribute .

groovy Medium getter and setter It's all implicit methods .

grovvy Class method instance :

class Account1 {
def number
def balance
def credit(amount){
balance+=amount
}
def debit(amount){
if(balance>=amount){
balance=amount
} }
def display(){
println "Account:${number} with balance:${balance}"
}
} def acc=new Account1(number:'123Abc',balance: 1200)
acc.display() acc.credit(200)
acc.display() acc.debit(900)
acc.debit(700)
acc.display()

Nested class instances

package com.andy.test

/**
* Created by admin on 16/5/6.
*/
class Account1 {
def number
def balance def credit(amount) {
balance += amount
} def debit(amount) {
if (balance >= amount) {
balance = amount
} } def display() {
println "Account:${number} with balance:${balance}"
} String toString() {
return "Account:${number} with balance:${balance}" }
} class Bank{
def openAccount(number,balance){
def acc=new Account1(number:number,balance:balance)
accounts[number]=acc
} def creditAccount(number ,amount){
def acc=this.findAccount(number)
if(acc!=null){
acc.credit(amount)
} } def getAccountBalance(number){
def acc=this.findAccount(number)
return (acc==null)? null:acc.balance
} def getTotalAssets(){
def total=0;
accounts.each {number,account->total+=account.balance}
return total
} def findAccount(number){
def acc=accounts.find{entry->entry.key==number}
return (acc==null)?null:acc.value
}
def name
def accounts=[:]
} def bk=new Bank(name:'Community')
bk.openAccount('123ABC',1200)
bk.openAccount('456DEF',1000)
bk.openAccount('789GHI',2000) bk.creditAccount('123ABC',300)
println "Balance for account 123ABC is :${bk.getAccountBalance('123ABC')}"
 

Grovvy Step byStep Examples More articles about

  1. Siebel The development of specification

    Siebel Configuration and Development Guideline 1 2 2.1 2.2 2.3 11. 2.4 2.5 3 3.1 3.2 3.2.1 3.2.2 3.3 ...

  2. INFORMATICA The deployment and implementation of BACKUP&amp;RESTORE

    Be a set of BI When the solution matures and runs , The company will rapidly expand its customer base , At this time, the rapid development of SOLUTION It's important to apply it to a new production environment , Let's talk about my project (INFORMATICA BACKUP&RE ...

  3. [ My favorite collection ]OpenCV part

    OpenCV Chinese Forum OpenCV Forum opencv Video tutorial Directory ( primary ) OpenCV course Opencv Thoughts and some sharing tornadomeet  Super God [ digital image processing ]C++ Read . Rotate and save bm ...

  4. uva 1631

    1631 Locker A password locker with N digits, each digit can be rotated to 0-9 circularly. You can ro ...

  5. Mod in math

    An Introduction to Modular Math When we divide two integers we will have an equation that looks like ...

  6. [ Collection reprint link ]Opencv part

    Reprinted from - Liu Rufeng -http://www.cnblogs.com/rongfangliu/p/opencvlink.html [ My favorite collection ]OpenCV part   OpenCV Chinese Forum OpenCV Forum o ...

  7. HDU 4433 locker(DP)(2012 Asia Tianjin Regional Contest)

    Problem Description A password locker with N digits, each digit can be rotated to 0-9 circularly.You ...

  8. HDU 4433 locker(12 Tianjin ,DP)

    4576 njczy2010 C Accepted 860 KB 140 ms G++ 2063 B 2014-10-16 09:51:19 Ah , Why? 1000*100*100 Of complexity dp I dare not knock ...

  9. [ turn ]Bootstrap 3.0.0 with ASP.NET Web Forms – Step by Step – Without NuGet Package

    In this paper, from :http://www.mytecbits.com/microsoft/dot-net/bootstrap-3-0-0-with-asp-net-web-forms In my earlier ...

Random recommendation

  1. SSH Interview questions (struts2+Spring+hibernate)

    struts2 + Spring +hibernate Hibernate How it works and why it works ?   principle :   1. Read and parse configuration file   2. Read and parse the mapping information , establish SessionFactory ...

  2. hdu 3341(ac automata + State compression )

    The question : Easy to understand ... Ideas : First of all, it's easy to think of using dp, Set up a dp[41][41][41][41][501] Array to solve , But obviously the memory is out of range , So I thought about how to reduce the memory ? As long as you know A.T.C.G ...

  3. iOS Network layer architecture design sharing

    Preface A few days ago, I helped the company restructure the network layer , At that time, I wanted to share it with you , And then we did a new version of the requirements , Now I have time to sort it out . The previous network layer used the method of drag and drop to import the project AF, And then a lot of source code changes , Time interval 2 year ...

  4. Windows To configure JAVA Environment variables of

    Java By Sun The company developed a programming language for distributed network environment ,Java Languages have cross platform features , It compiles programs that can run on a variety of operating system platforms , Can achieve “ Write once , Run anywhere ” The power of . Tools / raw material JD ...

  5. JBoss Series 70 : A simple CDI Web application

    summary This article passes a simple CDI Web Application demonstration dependency injection, scope, qualifiers as well as EL Integrate . After the application is deployed, we can go through http://localhos ...

  6. Redis Cache service construction and data reading and writing -- Reprint

    come from http://www.cnblogs.com/lc-chenlong/p/3218157.html 1.  Download and install Redis Download address :https://github.com/MSOpenTec ...

  7. .Net Programmers learn to use Oracle series (20): Hierarchical query (CONNECT BY)

    1. Hierarchical query statements 1.1.CONNECT BY grammar 1.2.CONNECT BY Example 2. Hierarchical query function 2.1.SYS_CONNECT_BY_PATH 2.2.WMSYS.WM_CONCAT 2. ...

  8. Readis For Windows Installation and password 、IP Limit

    One . Download and install Readis Github Download address :https://github.com/MicrosoftArchive/redis/tags download .MSI The next step is to install it Two . Verify the installation 1. stay &q ...

  9. Java Implementation of the singleton pattern

    Implementation of single case The problem of singleton design pattern 1.       Thread safety issues with synchronized Modify the instantiation part of the code 2.       Performance issues – Use lazy instantiation 3.       Command reordering problem – use volatile ...

  10. Python Crawler's multi thread download Douban Top250 Movie pictures

    Introduction to the crawler project    This crawler project will take Douban Top250 Pictures of the movie , Its website is :https://movie.douban.com/top250, The specific page is shown in the figure below :    This crawler project will not use multithreading and make ...