Operator Overloading क्या है
In this page:
class ClassName(val value: Type) {
operator fun plus(other: ClassName): ClassName = ClassName(/* combine */)
operator fun get(index: Int): Type = value
override fun equals(other: Any?): Boolean = /* compare */
}
a + b
a[0]
Overloading the Plus Operator
किसी class के अंदर operator fun plus(other: Type) define करना उस class के instances को सीधे + symbol से combine होने देता है।
उदाहरण: Overloading the Plus Operator
// Define a data class `Point`
data class Point(val x: Int, val y: Int) {
operator fun plus(other: Point): Point = Point(x + other.x, y + other.y)
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `p1`, set to `Point(1, 2)`
val p1 = Point(1, 2)
// Declare a read-only variable `p2`, set to `Point(3, 4)`
val p2 = Point(3, 4)
// Print `p1 + p2` to the console, with a trailing newline
println(p1 + p2)
}
Login to try C/C++/Java/PHP code in the editor
Overloading Comparison with equals
equals() को override करना (जो data classes अपने आप करती हैं) यह define करता है कि किसी type के लिए == का क्या मतलब है, दो अलग instances को उनके content के आधार पर logically बराबर माना जाने देते हुए।
उदाहरण: Overloading Comparison with equals
// Define a class `Money`
class Money(val cents: Int) {
override fun equals(other: Any?): Boolean {
// Return `other is Money && other.cents == cents` from this function
return other is Money && other.cents == cents
}
override fun hashCode(): Int = cents
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `a`, set to `Money(500)`
val a = Money(500)
// Declare a read-only variable `b`, set to `Money(500)`
val b = Money(500)
// Print "Equal: ${a == b}" to the console, with a trailing newline
println("Equal: ${a == b}")
}
Login to try C/C++/Java/PHP code in the editor
Overloading Index Access with get
operator fun get(index: Type) define करना किसी class के instances को square-bracket indexing support करने देता है, बिल्कुल किसी List या Array की तरह।
उदाहरण: Overloading Index Access with get
// Define a class `Grid`
class Grid(val values: List<List<Int>>) {
operator fun get(row: Int, col: Int): Int = values[row][col]
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `grid`, set to `Grid(listOf(listOf(1, 2), listOf(3, 4)))`
val grid = Grid(listOf(listOf(1, 2), listOf(3, 4)))
// Print "Value at (1, 0): ${grid[1, 0]}" to the console, with a trailing newline
println("Value at (1, 0): ${grid[1, 0]}")
}
Login to try C/C++/Java/PHP code in the editor
Making an Object Callable with invoke
operator fun invoke(...) define करना किसी class के instance को object के नाम के ठीक बाद parentheses उपयोग करते हुए सीधे किसी function की तरह call होने देता है।
उदाहरण: Making an Object Callable with invoke
// Define a class `Greeter`
class Greeter(val greeting: String) {
operator fun invoke(name: String): String = "$greeting, $name!"
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `greeter`, set to `Greeter("Hello")`
val greeter = Greeter("Hello")
// Print `greeter("Kotlin")` to the console, with a trailing newline
println(greeter("Kotlin"))
}
Login to try C/C++/Java/PHP code in the editor
- किसी operator convention से मेल खाने के लिए नामित function से पहले
operatorkeyword भूल जाना, जैसेplus, जो overload के असल में काम करने के लिए ज़रूरी है। - किसी operator को इस तरह overload करना जो users को हैरान करे, जैसे
+को subtraction करवाना, जो readability को मदद करने के बजाय नुकसान पहुंचाता है। - overload किए जा रहे operator के लिए exact expected function signature (name और parameter count) से न मिलाना।
+,-,*, और==जैसे operators specific function names (plus,minus,times,equals) से map होते हैं जिन्हेंoperatorkeyword से overload किया जा सकता है।- Overloaded operator functions को
operator funचिह्नित होना चाहिए और उस operator के लिए exact expected signature का पालन करना चाहिए। []access को overload करने के लिएget/setउपयोग होते हैं, और किसी object को function की तरह call करने के लिएinvokeउपयोग होता है।- Operator overloads को code को readable रखने के लिए operator के normal, expected meaning से मेल खाने वाले तरीके से व्यवहार करना चाहिए।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: