Constructors क्या हैं
In this page:
class ClassName(val property: Type, param: Type) { // primary constructor
constructor(other: Type) : this(other, defaultValue) {
// secondary constructor
}
}
The Primary Constructor
Primary constructor खुद class header का हिस्सा है, class name के ठीक बाद एक instance बनाने के लिए ज़रूरी parameters list करते हुए।
उदाहरण: The Primary Constructor
// Define a class `Book`
class Book(val title: String, val author: String)
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `book`, set to `Book("Kotlin in Action", "Dmitry Jemerov")`
val book = Book("Kotlin in Action", "Dmitry Jemerov")
// Print "${book.title} by ${book.author}" to the console, with a trailing newline
println("${book.title} by ${book.author}")
}
Login to try C/C++/Java/PHP code in the editor
Secondary Constructors
class body के अंदर एक constructor keyword instance बनाने का एक वैकल्पिक तरीका define करता है; हर secondary constructor को आखिरकार : this(...) का उपयोग करके primary constructor को delegate करना चाहिए।
उदाहरण: Secondary Constructors
// Define a class `Book`
class Book(val title: String, val author: String) {
constructor(title: String) : this(title, "Unknown")
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `book`, set to `Book("Kotlin Basics")`
val book = Book("Kotlin Basics")
// Print "${book.title} by ${book.author}" to the console, with a trailing newline
println("${book.title} by ${book.author}")
}
Login to try C/C++/Java/PHP code in the editor
Constructor Parameters vs Properties
primary constructor में बिना val/var के listed एक parameter सिर्फ constructor और किसी init blocks के अंदर उपलब्ध है -- इसे member के रूप में उजागर करने के लिए, इसे val या var declare करना चाहिए, या explicitly किसी property को assign करना चाहिए।
उदाहरण: Constructor Parameters vs Properties
// Define a class `Circle`
class Circle(radius: Double) {
// Declare a read-only variable `area` (a Double), set to `Math.PI * radius * radius`
val area: Double = Math.PI * radius * radius
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `circle`, set to `Circle(2.0)`
val circle = Circle(2.0)
// Print "Area: ${circle.area}" to the console, with a trailing newline
println("Area: ${circle.area}")
}
Login to try C/C++/Java/PHP code in the editor
Default Values in Constructors
बिल्कुल regular functions की तरह, constructor parameters के default values हो सकते हैं, instance बनाते समय कुछ arguments को छोड़ा जा सकने देते हुए।
उदाहरण: Default Values in Constructors
// Define a class `User`
class User(val name: String, val role: String = "member")
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `admin`, set to `User("Ana", "admin")`
val admin = User("Ana", "admin")
// Declare a read-only variable `user`, set to `User("Bo")`
val user = User("Bo")
// Print "${admin.name}: ${admin.role}" to the console, with a trailing newline
println("${admin.name}: ${admin.role}")
// Print "${user.name}: ${user.role}" to the console, with a trailing newline
println("${user.name}: ${user.role}")
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि जब एक primary constructor मौजूद हो तो secondary constructor को
: this(...)से इसे delegate करना चाहिए। - shared setup को primary constructor या एक
initblock में रखने के बजाय कई secondary constructors में validation logic दोहराना। - constructor parameters को properties के साथ confuse करना; बिना
val/varके एक साधारण parameter constructor के बाहर accessible नहीं है जब तक store न किया जाए।
- Primary constructor class header में, class name के ठीक बाद declare होता है।
- Secondary constructors class body के अंदर
constructorkeyword से declare होते हैं और उन्हें primary constructor को delegate करना चाहिए। - बिना
val/varके constructor parameters सिर्फ constructor/init block के अंदर उपयोग होने योग्य हैं, class members के रूप में नहीं। - कई secondary constructors किसी class को common setup logic share करते हुए अलग-अलग तरीकों से बनने देते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: