Groovy 学习笔记 (二)

本文深入探讨了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,stringArray

a[b] = c

Subscript assignment

a.puAt(b,c)

Object,list,map,StringBuffer,

Array

a << b

Left shift

a.leftShift(b)

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

Any type

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)

a <=> b

Spaceship

a.compareTo(b)

java.lang.Comparable

 

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')

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值