Android Groovy 特殊语法(二)

首先申明下,本文为笔者学习《Groovy 程序设计》的笔记,并加入笔者自己的理解和归纳总结。

1. 多赋值

Groovy可以返回一个数组,然后赋值给左侧表达式。左侧表达式必须以逗号分隔,放在圆括号中。

def splitName(fullname) {
   fullname.split(',')
}

(firstName, lastName) = splitName("Michael,Jordan")
print "$lastName, $firstName" // Jordan, Michael

使用数组来赋值

交换两个变量的值。

name1 = "Michael"
name2 = "James"
(name1, name2) = [name2, name1]
println "$name1, $name2" // James, Michael

左侧较少时,多余值会被抛弃

(name1, name2, name3) = ["Michael", "James", "Kavin", "Steven"]
println "$name1, $name2, $name3" // Michael, James, Kavin

左侧较多时,被设置为null

(name1, name2, name3) = ["Michael", "James"]
println "$name1, $name2, $name3" // Michael, James, null

2. 相等操作

Groovy将==映射到equals()方法,而is()方法映射到==方法。

name1 = "Michael"
name2 = new String("Michael")
name3 = name2

println name1 == name2 // true
println name2 == name3 // true
println name1.is(name2) // false
println name2.is(name3) // true

class A {
    boolean equals(other) {
        println "equals called"
        true
    }
}
new A() == new A() // equals called

如果实现了Comparable接口,==会被映射到该类的compareTo()方法。

class A implements Comparable {
    boolean equals(other) {
        println "equals called"
        true
    }

    int compareTo(other) {
        println "compareTo called"
        return 1
    }
}
new A() == new A() // compareTo called

3. 可选形参

Groovy中方法和构造器的形参是可选的。

设置默认参数。

def percent(x, max = 100.0) {
    x / max * 100 + "%"
}

println percent(23) // 23.00%
println percent(47, 100) // 47.00%
println percent(31, 1000) // 3.100%

使用数组实现可变长度的参数。

def log(msg, String[] details) {
    println "$msg - ${details}"
}

log "msg" // msg - []
log "msg", "detail1" // msg - [detail1]
log "msg", "detail1", "detail2" // msg - [detail1, detail2]

使用省略号(...)来传递可变长度的参数。

def log(msg, String... details) {
    println "$msg - ${details}"
}
log "msg" // msg - []
log "msg", "detail1" // msg - [detail1]
log "msg", "detail1", "detail2" // msg - [detail1, detail2]
log "msg", ["detail1", "detail2"] as String[] // msg - [detail1, detail2]

4. 安全导航操作符

安全导航操作符(?.)可以检查引用是否为空

def upperCase(str) {
    str?.toUpperCase()
}

println upperCase("hello world!") // HELLO WORLD!
println upperCase(null) // null

如果不使用(?.),会返回NullPointerException异常。

5. 循环

for-each循环

  • 使用冒号形式的for循环,必须指定name的类型。
    names = ["Michael", "James", "Kavin", "Steven"]
    for (String name : names) {
        println name
    }
    
    返回
    Michael
    James
    Kavin
    Steven
    
  • 使用in形式的for循环
    for (name in names) {
        println name
    }
    
    返回
    Michael
    James
    Kavin
    Steven
    

整型方法循环

  • 范围
    for (i in 1..3) {
        println i
    }
    
    返回
    1
    2
    3
    
  • upto()downto()方法
    1.upto(3) {
        println it
    }
    3.downto(1) {
        println it
    }
    
    返回
    1
    2
    3
    3
    2
    1
    
  • times()方法
    3.times {
        println it
    }
    
    返回
    0
    1
    2
    
  • step()方法
    1.step(7, 2) {
        println it
    }
    
    返回
    1
    3
    5
    

6. 文件File

Groovy中是File类来处理文件,大大简化了操作。

new File(fileName).text // 直接得到文件内容

如果想要逐行处理,调用eachLine()filterLine()等方法。

new File(fileName).eachLine {
    println it
}
println new File(fileName).filterLine {
    it =~ /File/
}

File还提供了很多withXX方法来处理文件的输入输出。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值