Class के Basics
In this page:
class ClassName {
fun method() { }
}
class ClassName(val property: Type, var other: Type)
val instance = ClassName(value1, value2)
Declaring a Simple Class
एक class संबंधित data और behavior को एक नाम के तहत group करता है, class keyword से defined। बिना किसी properties या methods वाली class को भी declare और instantiate किया जा सकता है।
उदाहरण: Declaring a Simple Class
// Define a class `Greeter`
class Greeter {
// Define the function `greet` with no parameters
fun greet() {
// Print "Hello from Greeter!" to the console, with a trailing newline
println("Hello from Greeter!")
}
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `greeter`, set to `Greeter()`
val greeter = Greeter()
greeter.greet()
}
Login to try C/C++/Java/PHP code in the editor
Properties in the Primary Constructor
किसी class के parentheses में सीधे val या var के साथ parameters declare करना अपने आप properties बना देता है, अलग field declarations और assignments लिखने की ज़रूरत से बचते हुए।
उदाहरण: Properties in the Primary Constructor
// Define a class `Dog`
class Dog(val name: String, val breed: String)
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `dog`, set to `Dog("Rex", "Labrador")`
val dog = Dog("Rex", "Labrador")
// Print "${dog.name} is a ${dog.breed}" to the console, with a trailing newline
println("${dog.name} is a ${dog.breed}")
}
Login to try C/C++/Java/PHP code in the editor
Creating Instances
किसी class का instance उसके नाम को किसी function की तरह call करके बनाया जाता है, उसके primary constructor के parameters से मेल खाते arguments पास करते हुए।
new keyword की ज़रूरत नहीं।उदाहरण: Creating Instances
// Define a class `Point`
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(3, 4)`
val p2 = Point(3, 4)
// Print "p1=(${p1.x}, ${p1.y}), p2=(${p2.x}, ${p2.y})" to the console, with a trailing newline
println("p1=(${p1.x}, ${p1.y}), p2=(${p2.x}, ${p2.y})")
}
Login to try C/C++/Java/PHP code in the editor
Member Functions
किसी class body के अंदर declared functions member functions (methods) हैं जो उस instance की अपनी properties पर operate करते हैं, implicitly this के ज़रिए accessible।
उदाहरण: Member Functions
// Define a class `Circle`
class Circle(val radius: Double) {
// Define the function `area` with no parameters and returning a Double
fun 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(3.0)`
val circle = Circle(3.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
- यह भूल जाना कि
classkeyword को एक primary constructor के लिए parentheses()चाहिए भले ही इसमें parameters हों, या कोई न होने पर उन्हें पूरी तरह छोड़ देना। - यह न समझना कि primary constructor में
val/varके साथ declared properties अपने आप accessible class members बन जाती हैं। - एक object बनाना लेकिन constructor call करते समय
()भूल जाना, जैसेDog()के बजायDogलिखना।
- एक class
classkeyword के बाद एक नाम और, optionally, एक primary constructor के साथ declare होती है। - constructor parentheses के अंदर
val/varसे declared properties अपने आप class members बन जाती हैं। - किसी class का एक object इसके constructor को किसी function की तरह call करके बनाया जाता है:
ClassName(...)। - Kotlin classes default रूप से
finalहैं (subclass नहीं की जा सकतीं) जब तकopenचिह्नित न हों।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: