Groovy基础语法

开始学习Groovy
内容主要来源于 http://www.ibm.com/developerworks/cn/java/j-groovygrails/index.html
本文主要涉及一些简单语法和概念

  1. package study
  2. /**
  3.  * Groovy的对象
  4.  */
  5. public class GroovyObject{
  6.     static void main(args){
  7.         println 12.class //一切都是对象
  8.         println '14'.class
  9.         def 年份 = 2008 //可以使用中文变量
  10.         println 年份
  11.     }
  12. }
  13. /**
  14.  * Groovy的语法
  15.  */
  16. public class GroovySyntax{
  17.     static void main(args){
  18.         def g = new GroovySyntax()
  19.         g.repeat('abc')
  20.         g.repeat('xyz',2)
  21.     }
  22.     
  23.     def repeat(val, repeat=5){
  24.         for(i in 0..<repeat){ //如果使用0..repeat,则会多循环一次
  25.             println val
  26.         }
  27.     }
  28. }
  29. /**
  30.  * Groovy的集合
  31.  */ 
  32. public class GroovyCollection{
  33.     static void main(args){
  34.         def range = 0..4
  35.         println range.class //集合类 class groovy.lang.IntRange
  36.         assert range instanceof List //断言
  37.         
  38.         def coll = ["Groovy""Java""Ruby"]
  39.         assert  coll instanceof Collection //col1是Collection的实例
  40.         assert coll instanceof ArrayList //col1是ArrayList的实例
  41.         
  42.         //三种增加方法:
  43.         coll.add("Python")
  44.         coll << "Smalltalk" //<< 操作符被重载,以支持向集合添加项
  45.         coll[5] = "Perl"
  46.         coll.each { println it} //使用闭包遍历coll,it是关键字,下行和本行效果一样
  47.         coll.each { param -> println(param)}
  48.         
  49.         def numbers = [1,2,3,4//使用+ - 增删集合的内容
  50.         assert numbers + 5 == [1,2,3,4,5]
  51.         assert numbers - [2,3] == [1,4]
  52.         def numbers1 = [1,2,3,4]
  53.         assert numbers1.join(".") == "1.2.3.4" //连接数组的内容
  54.         assert [1,2,3,4,3].count(3) == 2 //计算3的数量
  55.         assert ["JAVA""GROOVY"] == ["Java""Groovy"]*.toUpperCase() // * 表示数组的每个项
  56.     }
  57. }
  58. /**
  59.  * Groovy的Hash
  60.  */
  61. public class GroovyHashtable{
  62.     static void main(args){
  63.         def hash = [name:"Andy""VPN-#":45//Groovy会自动把name变成"name"
  64.         assert hash.getClass() == java.util.LinkedHashMap
  65.         hash.put("id"23//使用put方法
  66.         assert hash.get("name") == "Andy"
  67.         
  68.         hash.dob = "01/29/76" //相当于.put方法
  69.         assert hash.dob == "01/29/76"
  70.         
  71.         hash["gender"] = "male" //也可以使用[]代替put
  72.         assert hash.gender == "male"
  73.         hash.each{ param -> println(param.key); println(param.value)} //遍历map,注意,必须加;或者换行
  74.         
  75.         hash.each{ key, value -> println "${key} is ${value}" } //使用${}进行替换
  76.     }
  77. }
  78. /**
  79.  * Groovy的闭包
  80.  */
  81. public class GroovyClosure{
  82.     static void main(args){
  83.         def excite = { word ->  return "${word}!!"  }
  84.         
  85.         assert "Groovy!!" == excite("Groovy")
  86.         assert "Java!!" == excite.call("Java"//闭包可以传递给某变量,并且延时执行
  87.     }
  88. }
  89. /**
  90.  * Groovy的类
  91.  */
  92. public class GroovyClass{
  93.     static void main(args) {
  94.         def sng = new Song(name:"Le Freak"
  95.                 artist:"Chic", genre:"Disco"//自动构造函数
  96.                 
  97.         def sng3 = new Song() 
  98.         sng3.name = "Funkytown"
  99.         sng3.artist = "Lipps Inc."
  100.         sng3.setGenre("Disco"//也可以使用set或者属性设置值
  101.                 
  102.         assert sng3.getArtist() == "Lipps Inc."
  103.         
  104.         sng3.setGenre "Disco" //还可以这样
  105.         assert sng3.genre == "Disco"
  106.     }
  107.     
  108.     String toString(){
  109.         "${name}, ${artist}, ${genre}" //覆盖toString方法,最后一行默认是return语句
  110.     }
  111.     
  112. }
  113. public class Song{
  114.     def name
  115.     def artist
  116.     def genre
  117.     def getGenre(){
  118.         genre?.toUpperCase() //使用?判断非空
  119.     }
  120. }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值