Grovvy Step byStep Examples
def LIMIT=10
def count=1
println 'start'
while(count<=LIMIT){
println "count:${count}"
count++
}
println 'Done'
注意:固定变量使用大写编写
for语句:
def LIMIT =10;
println 'start'
for (count in 1..LIMIT){
println "${count}"
}
println 'done'
循环处理列表:
println 'start'
for (count in [11,12,13,14]){
println "${count}"
}
println 'done'
循环处理映射:
def staff=['ken':21,'john':25,'sally':22]
def totalAge=0;
for(staffEntry in staff){
totalAge+=staffEntry.value
} print "total age is ${totalAge}"
循环处理字符串:
def name='Kenneth'
def listofChar=[]
for(letter in name){
listofChar << letter
}
println "listofChar:${listofChar}"
if判断:
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和范围:
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和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 }
闭包及其调用:
def clos={println 'Hello world'}
clos.call()
参数化的闭包:
def clos={param->println "Hello ${param}"}
clos.call('Andy')
单个隐参数:
def clos={println "Hello ${it}"}
clos.call('Andy')
其中的${it}中的it代表iterator
each方法和闭包:
[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}"}
条件元素:
[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和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方法:
def list=[1,2,3,4].collect{element->return element*element}
println "list:${list}"
collect高级应用:
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 Account1 {
def number
def balance
}
def acc=new Account1(number:'ACB123',balance:1200)
println "Account ${acc.number} has balance ${acc.balance}"
注意:acc.number用了访问Account对象的acc的number属性。
groovy中的getter和setter都是隐含方法。
grovvy类方法实例:
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()
嵌套类实例
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的更多相关文章
- Siebel 开发规范
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 ...
- INFORMATICA 的部署实施之 BACKUP&RESTORE
当一套BI 解决方案成熟运行后,公司会快速扩大客户群,这时快速的将开发出来的SOLUTION 应用到全新的生产环境中就很重要了,下面谈谈我做这样项目(INFORMATICA BACKUP&RE ...
- [收藏夹整理]OpenCV部分
OpenCV中文论坛 OpenCV论坛 opencv视频教程目录(初级) OpenCV 教程 Opencv感想和一些分享 tornadomeet 超牛的大神 [数字图像处理]C++读取.旋转和保存bm ...
- uva 1631
1631 Locker A password locker with N digits, each digit can be rotated to 0-9 circularly. You can ro ...
- Mod in math
An Introduction to Modular Math When we divide two integers we will have an equation that looks like ...
- [收藏转载链接]Opencv部分
转载自-柳如风-http://www.cnblogs.com/rongfangliu/p/opencvlink.html [收藏夹整理]OpenCV部分 OpenCV中文论坛 OpenCV论坛 o ...
- 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 ...
- HDU 4433 locker(12年天津,DP)
4576 njczy2010 C Accepted 860 KB 140 ms G++ 2063 B 2014-10-16 09:51:19 哎,为啥1000*100*100的复杂度的dp就不敢敲了呢 ...
- [转]Bootstrap 3.0.0 with ASP.NET Web Forms – Step by Step – Without NuGet Package
本文转自:http://www.mytecbits.com/microsoft/dot-net/bootstrap-3-0-0-with-asp-net-web-forms In my earlier ...
随机推荐
- 2016开发一个app需要多少钱?app开发需要哪些成本-app开发问题汇总-广州达到信息
作为一个APP开发从业者,被外行的朋友们问及最多的问题是,"做一个网站需要多少钱?"或者"开发一个APP需要多少钱?".作为开发过完整网站项目和手机APP的人, ...
- uva 524 prime ring problem——yhx
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural ...
- 无向图的最短路径算法JAVA实现
一,问题描述 给出一个无向图,指定无向图中某个顶点作为源点.求出图中所有顶点到源点的最短路径. 无向图的最短路径其实是源点到该顶点的最少边的数目. 本文假设图的信息保存在文件中,通过读取文件来构造图. ...
- python中for和if else的使用
In []: a = set('abcd') In []: b = set('ef') In []: def match(x,y): ....: for i in x: ....: for j in ...
- Django中的syncdb命令
从官方文档的意思来看,现在他已经成为migrate命令的同义词了,和migrate命令有相同的作用. Deprecated since version 1.7: This command has be ...
- YOU ARE MY SUNSHINE
/*you are sunshine, my only sunshine, you make me happy when skies are grey. you'll never know dear ...
- java11-3 String类的获取功能
String类的获取功能 int length():获取字符串的长度. char charAt(int index):获取指定索引位置的字符 int indexOf(int ch):返回指定字符在此字 ...
- Python-json 和 pickle
这是用于序列化的两个模块 json:用于字符串和python数据类型间进行转换 pickle:用于python特有的类型和python的数据类型间进行转换 json模块提供了四个功能:dumps du ...
- 一篇搞定RSA加密与SHA签名|与Java完全同步
基础知识 什么是RSA?答:RSA是一种非对称加密算法,常用来对传输数据进行加密,配合上数字摘要算法,也可以进行文字签名. RSA加密中padding?答:padding即填充方式,由于RSA加密算法 ...
- sasscore学习之_mixin.scss
_mixin scss包括常用的mixin,%及@functionmixin,通过@include调用,样式通过拷贝的方式使用,尤其适用于传递参数%,通过@extend调用,样式通过组合申明的方式使用 ...