Generic Classes क्या हैं
In this page:
class ClassName<T>(val item: T) {
fun get(): T = item
}
val a = ClassName(value) // T inferred
val b = ClassName<Type>(value) // explicit type argument
Declaring a Generic Class
class name के बाद angle brackets में एक type parameter, जैसे <T>, उस class को किसी भी type के साथ काम करने देता है, जहां भी इसके अंदर उपयोग हो वहां T के रूप में भरा जाता है।
उदाहरण: Declaring a Generic Class
// Define a class `Box`
class Box<T>(val item: T)
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `intBox`, set to `Box(42)`
val intBox = Box(42)
// Declare a read-only variable `stringBox`, set to `Box("Kotlin")`
val stringBox = Box("Kotlin")
// Print "${intBox.item}, ${stringBox.item}" to the console, with a trailing newline
println("${intBox.item}, ${stringBox.item}")
}
Login to try C/C++/Java/PHP code in the editor
Type Inference for Generics
Kotlin आमतौर पर constructor को पास की गई value से type argument को अपने आप infer कर लेता है, इसलिए आपको शायद ही कभी explicitly Box<Int> लिखने की ज़रूरत पड़े।
उदाहरण: Type Inference for Generics
// Define a class `Pair2`
class Pair2<A, B>(val first: A, val second: B)
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `pair`, set to `Pair2("age", 30)`
val pair = Pair2("age", 30)
// Print "${pair.first} = ${pair.second}" to the console, with a trailing newline
println("${pair.first} = ${pair.second}")
}
Login to try C/C++/Java/PHP code in the editor
Explicit Type Arguments
जब compiler के पास infer करने के लिए कुछ न हो, जैसे एक खाली generic container बनाना, type argument को angle brackets में explicitly प्रदान करना चाहिए।
उदाहरण: Explicit Type Arguments
// Define a class `Container`
class Container<T> {
private val items = mutableListOf<T>()
fun add(item: T) = items.add(item)
// Define the function `all` with no parameters and returning a `List<T>`
fun all(): List<T> = items
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `container`, set to `Container<String>()`
val container = Container<String>()
// Add `"Kotlin"` to `container`
container.add("Kotlin")
// Add `"Generics"` to `container`
container.add("Generics")
// Print `container.all()` to the console, with a trailing newline
println(container.all())
}
Login to try C/C++/Java/PHP code in the editor
Multiple Type Parameters
एक generic class एक से ज़्यादा type parameters declare कर सकती है, इसे एक साथ कई स्वतंत्र placeholder types के साथ काम करने देते हुए।
उदाहरण: Multiple Type Parameters
// Define a class `KeyValue`
class KeyValue<K, V>(val key: K, val value: V) {
fun describe() = "$key -> $value"
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `entry`, set to `KeyValue("name", "Kotlin")`
val entry = KeyValue("name", "Kotlin")
// Print `entry.describe()` to the console, with a trailing newline
println(entry.describe())
}
Login to try C/C++/Java/PHP code in the editor
- एक generic
Box<T>के बजाय इसे जिस हर type को रखना चाहिए उसके लिए एक अलग class लिखना (जैसेIntBox,StringBox)। - जब type infer न किया जा सके तब type argument specify करना भूल जाना, जैसे infer करने के लिए बिना किसी elements के एक खाली generic collection बनाना।
- type parameter name (जैसे
T) को एक असली type के साथ confuse करना;Tबस एक placeholder नाम है, conventionally एक single capital letter।
- एक generic class अपने नाम के बाद angle brackets में एक या ज़्यादा type parameters declare करती है, जैसे
class Box<T>(val item: T)। - type parameter
Tएक placeholder की तरह काम करता है जो class उपयोग होने पर एक concrete type से replace हो जाता है। - Type arguments अक्सर constructor arguments से अपने आप infer होते हैं, लेकिन ज़रूरत पड़ने पर explicitly specify किए जा सकते हैं।
- Generic classes हर अलग type के लिए जिसे इन्हें रखना हो एक जैसा logic दोहराने से बचाती हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: