Object Declarations क्या हैं
In this page:
object ObjectName {
val property: Type = value
fun method() { }
}
ObjectName.property
ObjectName.method()
Declaring a Singleton
object Name { ... } एक साथ class और इसका single instance दोनों define करता है; कोई अलग constructor call नहीं होता -- आप इसे उपयोग करने के लिए बस सीधे Name को refer करते हैं।
उदाहरण: Declaring a Singleton
// Define the singleton object `AppConfig`
object AppConfig {
// Declare a read-only variable `appName`, set to `"MyApp"`
val appName = "MyApp"
// Declare a mutable variable `debugMode`, initialized to `false`
var debugMode = false
}
// Entry point: execution of the program starts here
fun main() {
// Print `AppConfig.appName` to the console, with a trailing newline
println(AppConfig.appName)
AppConfig.debugMode = true
// Print "Debug: ${AppConfig.debugMode}" to the console, with a trailing newline
println("Debug: ${AppConfig.debugMode}")
}
Login to try C/C++/Java/PHP code in the editor
Object Declarations for Utility Functions
Objects stateless utility functions के लिए एक स्वाभाविक जगह हैं जो logically साथ belong करते हैं लेकिन हर call के लिए एक instance की ज़रूरत नहीं।
उदाहरण: Object Declarations for Utility Functions
// Define the singleton object `MathUtils`
object MathUtils {
fun square(x: Int) = x * x
fun cube(x: Int) = x * x * x
}
// Entry point: execution of the program starts here
fun main() {
// Print "Square of 5: ${MathUtils.square(5)}" to the console, with a trailing newline
println("Square of 5: ${MathUtils.square(5)}")
// Print "Cube of 3: ${MathUtils.cube(3)}" to the console, with a trailing newline
println("Cube of 3: ${MathUtils.cube(3)}")
}
Login to try C/C++/Java/PHP code in the editor
Lazy Initialization
किसी object का instance पहली बार access होने पर बनता है, ज़रूरी नहीं कि program शुरू होने पर, जिसे इसके अपने init block से print करके देखा जा सकता है।
उदाहरण: Lazy Initialization
// Define the singleton object `Session`
object Session {
init {
// Print "Session object created" to the console, with a trailing newline
println("Session object created")
}
// Declare a read-only variable `id`, set to `"abc123"`
val id = "abc123"
}
// Entry point: execution of the program starts here
fun main() {
// Print "Before accessing Session" to the console, with a trailing newline
println("Before accessing Session")
// Print "Session id: ${Session.id}" to the console, with a trailing newline
println("Session id: ${Session.id}")
}
Login to try C/C++/Java/PHP code in the editor
Object Expressions for One-Off Instances
एक anonymous object : Type { ... } एक interface implement करता या class extend करता single-use instance बनाता है, एक पूरी named class declare किए बिना एक तेज़, throwaway implementation के लिए उपयोगी।
उदाहरण: Object Expressions for One-Off Instances
// Define an interface `ClickListener` describing required behavior
interface ClickListener {
// Define the function `onClick` with no parameters
fun onClick()
}
// Entry point: execution of the program starts here
fun main() {
// Call `object : ClickListener` with a trailing lambda, storing the result in `listener`
val listener = object : ClickListener {
override fun onClick() {
// Print "Clicked!" to the console, with a trailing newline
println("Clicked!")
}
}
listener.onClick()
}
Login to try C/C++/Java/PHP code in the editor
- किसी
objectdeclaration को constructor arguments पास करने की कोशिश करना;objectसे declared singletons constructor parameters नहीं ले सकते। - यह भूल जाना कि एक
objectपहली access पर lazily initialize होता है, program startup पर नहीं, जो इसकेinitblock में side effects के लिए मायने रख सकता है। objectdeclarations (named singletons) को one-off implementations के लिए उपयोग होने वाले anonymousobjectexpressions के साथ confuse करना।
object Name { ... }एक singleton declare करता है -- बिल्कुल एक instance जो पहली access पर lazily बनता है।- Singletons constructor parameters नहीं ले सकते क्योंकि इन्हें बनाने के लिए कोई explicit call नहीं होता।
- एक anonymous
object : SomeType { ... }(एक object expression) एक interface implement करता या class extend करता एक one-off instance बनाता है, अक्सर वहां उपयोग होता है जहां Java anonymous class उपयोग होती। - Object declarations आमतौर पर utility functions, constants, और shared state के लिए उपयोग होते हैं जो बिल्कुल एक बार मौजूद होनी चाहिए।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: