Skip to content

Instantly share code, notes, and snippets.

@mio4kon
Created August 29, 2016 05:59
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 mio4kon/a1e38de3b68d40fe7505399698257e90 to your computer and use it in GitHub Desktop.
Save mio4kon/a1e38de3b68d40fe7505399698257e90 to your computer and use it in GitHub Desktop.
Kotlin操作符重载
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