Data Classes क्या हैं
In this page:
data class ClassName(val property1: Type, val property2: Type)
val a = ClassName(value1, value2)
val b = a.copy(property2 = newValue)
val (x, y) = a
Declaring a Data Class
class से पहले data जोड़ना compiler से useful boilerplate -- equals(), hashCode(), और toString() -- generate करने के लिए कहता है, primary constructor में properties के आधार पर।
उदाहरण: Declaring a Data Class
// Define a data class `Point`
data class Point(val x: Int, val y: Int)
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `p`, set to `Point(3, 4)`
val p = Point(3, 4)
// Print `p` to the console, with a trailing newline
println(p)
}
Login to try C/C++/Java/PHP code in the editor
Automatic equals() and hashCode()
एक जैसी property values वाले दो data class instances को == द्वारा बराबर माना जाता है, क्योंकि equals() सभी constructor properties को compare करने के लिए generate होता है।
उदाहरण: Automatic equals() and hashCode()
// Define a data class `Point`
data class Point(val x: Int, val y: Int)
// 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(1, 2)`
val p2 = Point(1, 2)
// Print "Equal: ${p1 == p2}" to the console, with a trailing newline
println("Equal: ${p1 == p2}")
}
Login to try C/C++/Java/PHP code in the editor
Copying with Modifications
generated copy() function एक जैसी property values वाला एक नया instance बनाता है, आपको सिर्फ उन्हें override करने देते हुए जिन्हें आप बदलना चाहते हैं।
उदाहरण: Copying with Modifications
// Define a data class `User`
data class User(val name: String, val age: Int)
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `original`, set to `User("Ana", 30)`
val original = User("Ana", 30)
// Declare a read-only variable `older`, set to `original.copy(age = 31)`
val older = original.copy(age = 31)
// Print "$original -> $older" to the console, with a trailing newline
println("$original -> $older")
}
Login to try C/C++/Java/PHP code in the editor
Destructuring a Data Class
Data classes अपने आप destructuring declarations support करती हैं, आपको उनकी properties को उस order में अलग variables में unpack करने देते हुए जिस order में वे declared थीं।
उदाहरण: Destructuring a Data Class
// Define a data class `Point`
data class Point(val x: Int, val y: Int)
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `point`, set to `Point(5, 10)`
val point = Point(5, 10)
val (x, y) = point
// Print "x=$x, y=$y" to the console, with a trailing newline
println("x=$x, y=$y")
}
Login to try C/C++/Java/PHP code in the editor
- एक normal
classलिखना और manuallyequals,hashCode, औरtoStringimplement करना जबdata classतीनों मुफ़्त में generate कर देता। - यह भूल जाना कि
copy()एक shallow copy करता है, इसलिए एक ही nested object को reference करने वाली mutable properties अब भी original और copy के बीच share होती हैं। - properties को primary constructor के बाहर रखना और यह उम्मीद करना कि वे generated
equals/toStringमें शामिल होंगी; सिर्फ constructor properties शामिल होती हैं।
data classअपने आपequals(),hashCode(),toString(), और एकcopy()function generate करता है primary constructor properties के आधार पर।copy()एक जैसी values वाला एक नया instance बनाता है, optionally specific properties override करते हुए।- Data classes
componentN()functions के ज़रिए कई variables में destructuring भी support करती हैं। - सिर्फ primary constructor में declared properties generated functions में हिस्सा लेती हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: