Created
October 19, 2011 10:55
-
-
Save j5ik2o/1297966 to your computer and use it in GitHub Desktop.
Scala - メソッド
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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