티스토리 뷰
Operator Overloding
//example 1
operator fun Int.plus(b: Int) = this + b
//example 2
operator fun Int.plus(b: Any): String {
return "$this $b"
}
println(10 + "ABC") // 10 ABC
//example 3
data class Position(val a: Int, val b: Int) {
operator fun plus(item: Position): Position {
return Position(a + item.a, b + item.b)
}
}
val positionOne = Position(1, 2)
val positionTwo = Position(3, 4)
println(positionOne + positionTwo) // Position(a=4, b=6)
//example 4
data class Position(var a: Int, var b: Int) {
operator fun unaryMinus(): Position {
return Position(-a, -b)
}
}
println(-Position(1, 2)) // Position(a=-1, b=-2)
println(-Position(-20, -10)) // Position(a=20, b=10)
example 5
data class Position(var a: Int, var b: Int) {
operator fun inc(): Position {
return Position(a.inc(), b.inc())
}
}
var position1 = Position(1, 2)
var position2 = Position(8, 9)
println(position++) // Position(a=1, b=2)
println(++position) // Position(a=9 b=10)
Operator
operations | Expression | Transrated to | ex) |
Unary | +a | a.unaryPlus() | 왜 있는지 잘 모르겠다. |
-a | a.unaryMinus() | var a = 1 var b = -1 println(-a) // -1 println(-b) // 1 |
|
!a | a.not() | ||
Increments | a++ | a.inc() + see below | |
decrements | a-- | a.dec() + see below | |
Arithmetic | a + b | a.plus(b) | |
a - b | a.minus(b) | ||
a * b | a.times(b) | ||
a / b | a.div(b) | ||
a % b | a.rem(b) | ||
a .. b | a.rangeTo(b) | ||
in | a in b | b.contains(a) | |
a !in b | !b.contains(a) | ||
Indexed access | a[i] | a.get(i) | |
a[i, j] | a.get(i, j) | ||
a[i_1, ..., i_n] | a.get(i_1, ..., i_n) | ||
a[i] = b | a.set(i, b) | ||
a[i, j] = b | a.set(i, j, b) | ||
a[i_1, ..., i_n] = b | a.set(i_1, ..., i_n, b) | ||
invoke | a() | a.invoke() | |
a(i) | a.invoke(i) | ||
a(i, j) | a.invoke(i, j) | ||
a(i_1, ..., i_n) | a.invoke(i_1, ..., i_n) | ||
Augmented assignments | a += b | a.plusAssign(b) | |
a -= b | a.minusAssign(b) | ||
a *= b | a.timesAssign(b) | ||
a /= b | a.divAssign(b) | ||
a %= b | a.remAssign(b) | ||
Equality and inequality | a == b | a?.equals(b) ?: (b === null) | |
a != b | !(a?.equals(b) ?: (b === null)) | ||
Comparison | a > b | a.compareTo(b) > 0 | |
a < b | a.compareTo(b) < 0 | ||
a >= b | a.compareTo(b) >= 0 | ||
a <= b | a.compareTo(b) <= 0 |
'Programming > Kotlin' 카테고리의 다른 글
Kotlin Loop (0) | 2022.04.23 |
---|---|
Kotlin Conditions (0) | 2022.04.23 |
Kotlin Class (0) | 2022.04.16 |
Kotlin Visibility Modifiers (0) | 2022.04.16 |
Kotlin Variable (0) | 2022.04.16 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Property
- RetryTemplate
- Query DSL
- Registrar
- Spring
- Discriminate Mapping
- Mapping
- 복합키 Mapping
- Embedded Mapping
- docker
- Join Table
- Sprint RetryTemplate
- Akka
- spring spel
- SmartLifecycle
- @Primary
- Criteria
- JPA
- java Equals
- Embeddable Mapping
- Typesafe Config
- java generic
- Spring JDBC Template
- Charles proxy
- guava
- scikit-learn
- Spring Registrar
- DI
- java EqualsAndHashCode
- JPA Criteria
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함