Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Created October 19, 2011 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j5ik2o/1297966 to your computer and use it in GitHub Desktop.
Save j5ik2o/1297966 to your computer and use it in GitHub Desktop.
Scala - メソッド
class Money(val amount: BigDecimal, val currency: Currency){
def plus(other: Money) = {
require(other.currency == currency) // falseだとIllegalArgumentException
new Money(amount + other.amount, currency) // 最後の式で評価された値が戻り値
}
override def equals(obj: Any) = obj match { // 該当したcaseの=>の右側の式が評価され、その結果がmatch式の戻り値
case that: Money => amount == that.amount && currency == other.currency
case _ => false // defaultケースのようなもの
}
override def hashCode = amount.hashCode + currency.hashCode
// override def hashCode = amount.## + currency.## でもよい
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment