Created
August 29, 2016 05:59
-
-
Save mio4kon/a1e38de3b68d40fe7505399698257e90 to your computer and use it in GitHub Desktop.
Kotlin操作符重载
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
data class Money(val currency: String, val amount: Int) | |
operator fun Money.plus(other: Money): Money { | |
if (currency != other.currency) { | |
throw IllegalArgumentException("currency 不匹配!") | |
} | |
return copy(amount = amount + other.amount) | |
} | |
fun main(args: Array<String>) { | |
val one = Money("RMB", 100) | |
val two = Money("RMB", 200) | |
val three = one + two | |
println(three) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment