Extension Properties क्या हैं
In this page:
val Type.propertyName: PropType
get() = expression // this is the receiver
var Type.writable: PropType
get() = expression
set(value) { /* update the receiver */ }
Declaring an Extension Property
एक extension property किसी मौजूदा type में एक computed property जोड़ता है, एक custom get() के साथ लिखा गया क्योंकि इसके लिए एक असली backing field store करने का कोई तरीका नहीं।
उदाहरण: Declaring an Extension Property
val String.firstChar: Char
get() = this[0]
// Entry point: execution of the program starts here
fun main() {
// Print `"Kotlin".firstChar` to the console, with a trailing newline
println("Kotlin".firstChar)
}
Login to try C/C++/Java/PHP code in the editor
Extension Properties Must Be Computed
चूंकि किसी मौजूदा class में असली storage जोड़ने के लिए कोई जगह नहीं, हर extension property को हर बार access होने पर receiver से अपनी value निकालनी चाहिए।
उदाहरण: Extension Properties Must Be Computed
val List<Int>.average2: Double
get() = if (this.isEmpty()) 0.0 else this.sum().toDouble() / this.size
// Entry point: execution of the program starts here
fun main() {
// Print `listOf(2, 4, 6).average2` to the console, with a trailing newline
println(listOf(2, 4, 6).average2)
}
Login to try C/C++/Java/PHP code in the editor
Writable Extension Properties
एक var extension property get() और set(value) दोनों define कर सकती है, इसे एक computed read-write property की तरह व्यवहार करने देते हुए भले ही यह सीधे अपनी कोई state store न करे।
उदाहरण: Writable Extension Properties
var StringBuilder.lastChar: Char
get() = this[this.length - 1]
set(value) {
this.setCharAt(this.length - 1, value)
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `sb`, set to `StringBuilder("Kotlin!")`
val sb = StringBuilder("Kotlin!")
sb.lastChar = '?'
// Print `sb` to the console, with a trailing newline
println(sb)
}
Login to try C/C++/Java/PHP code in the editor
Extension Properties on Custom Types
Extension properties आपकी अपनी classes पर भी काम करती हैं, जो original class को modify किए बिना किसी object के data का एक derived, read-only view जोड़ने के लिए उपयोगी है।
उदाहरण: Extension Properties on Custom Types
// Define a data class `Rectangle`
data class Rectangle(val width: Double, val height: Double)
val Rectangle.area: Double
get() = width * height
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `rect`, set to `Rectangle(4.0, 5.0)`
val rect = Rectangle(4.0, 5.0)
// Print "Area: ${rect.area}" to the console, with a trailing newline
println("Area: ${rect.area}")
}
Login to try C/C++/Java/PHP code in the editor
- किसी extension property को एक backing field (एक initial stored value) देने की कोशिश करना; extension properties में backing fields नहीं हो सकते, सिर्फ computed getters (और setters)।
- यह भूल जाना कि एक extension
varproperty को customget()औरset()दोनों चाहिए क्योंकि default लेने के लिए कोई field नहीं। - महंगे computations के लिए extension properties का ज़्यादा उपयोग करना, यह भूलते हुए कि हर access value फिर से compute करता है क्योंकि कुछ भी cached नहीं होता।
- एक extension property
val/var ReceiverType.propertyName: Type get() = ...के रूप में declared होती है। - Extension properties में एक backing field नहीं हो सकता -- इन्हें एक custom getter (और
varके लिए setter) के ज़रिए compute होना चाहिए। - Extension functions की तरह, extension properties declared type के आधार पर statically resolved होती हैं।
- Extension properties किसी मौजूदा type में एक derived, read-only fact जोड़ने का एक साफ़ तरीका हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: