Kotlin miniguide #9 operator overloading

In Kotlin we can overload some operators


  class Set(val num : Int) {
      operator fun plus(s: Set) = Set(this.num+ s.num)
  }


And we can use it like this


Set s1 = Set(5)

Set s2 = Set(6)

Set s3 = s1 + s2

We have a new Set with 11 element.

Lascia un commento