Groovy 学习笔记 (三)
1. == 是判断是否 equals, 而判断是否是同一对象则用 is
2. list 和 map 的一些方法:
def x = 1..10
assert x.contains(5)
assert x.contains(15) == false
assert x.size() == 10
assert x.from == 1
assert x.to == 10
assert x.reverse() == 10..1
3. 以下对象都有 size() 方法
Array, String, StringBuffer, Collection, Map, File
4. 数据类型:12 – Integer, 100L – Long, 1.23F – Float, 1.23D – Double, 123G – BigInteger, 1.23,1.23G – BigDecimal
如果一个小数, 末尾什么字符都没加, 那么这个小数使用的是 BigDecimal.
5. 操作符重载
| Operator | Name | Method | Works with |
| a + b | Plus | a.plus(b) | Number,string,collection |
| a – b | Minus | a.minus(b) | Number,string,collection |
| a * b | Star | a.multiply(b) | Number,string,collection |
| a / b | Divide | a.div(b) | Number |
| a % b | Modulo | a.mod(b) | Integral number |
| a++ | Post increment | a.next() | Number,string,range |
| ++a | Pre increment | ||
| a– | Post decrement | a.previous() | Number,string,range |
| –a | Pre decrement | ||
| a**b | Power | a.power(b) | Number |
| a | b | Numerical or | a.or(b) | Integral number |
| a & b | Numerical and | a.and(b) | Integral number |
| a ^ b | Numerical xor | a.xor(b) | Integral number |
| ~a | Bitwise complement | a.negate() | Integral number,string (the latter returning a regular expression pattern) |
| a[b] | Subscript | a.getAt(b) | Object,list,map,string、Array |
| a[b] = c | Subscript assignment | a.puAt(b,c) | Object,list,map,StringBuffer, Array |
| a << b | Left shift | a.leftShift(b) | Integral number, also used like “append” to StringBuffers, Writers, Files, Sockets, Lists |
| a >> b | Right shift | a.rightShift(b) | Integral number |
| a >>> b | Right shift unsigned | a.rightShiftUnsigned(b) | Integral number |
| switch(a){ case b: } |
Classification | b.isCase(a) | Object, range, list, collection, pattern, closure; also used with collection c in c.grep(b), which returns all items of c where b.isCase(item) |
| a == b | Equals | a.equals(b) | Object, consider hashCode() |
| a != b | Not equals | !a.equals(b) | Object |
| a <=> b | Spaceship | a.compareTo(b) | java.lang.Comparable |
| a > b | Greater than | a.compareTo(b) > 0 | |
| a >= b | Greater than or equal to | a.compareTo(b) >= 0 | |
| a < b | Less than | a.compareTo(b) < 0 | |
| a <= b | Less than or equal to | a.compareTo(b) <= 0 | |
| a as type | Enforced coercion | a.asType(typeClass) | Any type |
class Money {
private int amount
private String currency
Money (amountValue, currencyValue) {
amount = amountValue
currency = currencyValue
}
boolean equals (Object other) {
if (null == other) return false
if (! (other instanceof Money)) return false
if (currency != other.currency) return false
if (amount != other.amount) return false
return true
}
int hashCode() {
amount.hashCode() + currency.hashCode()
}
Money plus (Money other) {
if (null == other) return null
if (other.currency != currency) {
throw new IllegalArgumentException("cannot add $other.currency to $currency")
}
return new Money(amount + other.amount, currency)
}
}
def buck = new Money(1, 'USD')
d Use overridden ==
assert buck
assert buck == new Money(1, 'USD')
e Use overridden +
assert buck + buck == new Money(2, 'USD')
6.
By javafuns on August 5, 2009 at 22:21 ·
Views: 399 · Permalink · RSS
Categorized in: Scripts · Tagged with: Groovy, Java, Scripts
Categorized in: Scripts · Tagged with: Groovy, Java, Scripts


(