Properties क्या हैं
In this page:
class ClassName {
var property: Type = initialValue
get() = field // custom getter
set(value) { field = value } // custom setter
val computed: Type
get() = expression
}
Automatic Getters and Setters
हर val/var property में compiler द्वारा generated एक getter (और, var के लिए, एक setter) अपने आप होता है, इसलिए आप इसे किसी method call करने के बजाय सीधे object.property से access करते हैं।
उदाहरण: Automatic Getters and Setters
// Define a class `Temperature`
class Temperature(var celsius: Double)
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `temp`, set to `Temperature(20.0)`
val temp = Temperature(20.0)
// Print "Celsius: ${temp.celsius}" to the console, with a trailing newline
println("Celsius: ${temp.celsius}")
temp.celsius = 25.0
// Print "Updated: ${temp.celsius}" to the console, with a trailing newline
println("Updated: ${temp.celsius}")
}
Login to try C/C++/Java/PHP code in the editor
Custom Getters
कोई property एक custom get() define कर सकती है जो उस value को सीधे store करने के बजाय दूसरी properties से तुरंत compute करता है।
उदाहरण: Custom Getters
// Define a class `Rectangle`
class Rectangle(val width: Double, val height: Double) {
val 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
Custom Setters
एक var property एक custom set(value) define कर सकती है जो हर बार एक नई value assign होने पर validation या transformation logic चलाता है।
field property की backing storage को refer करता है।उदाहरण: Custom Setters
// Define a class `Account`
class Account {
// Declare a mutable variable `balance` (a Double), initialized to `0.0`
var balance: Double = 0.0
set(value) {
field = if (value < 0) 0.0 else value
}
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `account`, set to `Account()`
val account = Account()
account.balance = -50.0
// Print "Balance: ${account.balance}" to the console, with a trailing newline
println("Balance: ${account.balance}")
}
Login to try C/C++/Java/PHP code in the editor
Restricting Setter Visibility
किसी var property के बाद private set जोड़ना दूसरे code को इसे स्वतंत्र रूप से पढ़ने देता है जबकि सिर्फ class खुद इसकी value बदल सकती है, जो read-only state को सुरक्षित रूप से उजागर करने का एक आम तरीका है।
उदाहरण: Restricting Setter Visibility
// Define a class `Counter`
class Counter {
// Declare a mutable variable `count` (an Int), initialized to `0`
var count: Int = 0
private set
// Define the function `increment` with no parameters
fun increment() {
count++
}
}
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `counter`, set to `Counter()`
val counter = Counter()
counter.increment()
counter.increment()
// Print "Count: ${counter.count}" to the console, with a trailing newline
println("Count: ${counter.count}")
}
Login to try C/C++/Java/PHP code in the editor
- Java की आदत से manual
getX()/setX()methods लिखना, जबकि Kotlin properties पहले से उन्हें अपने आप generate कर देती हैं। - यह भूल जाना कि एक custom getter हर बार access होने पर अपनी value फिर से compute करता है, अपने आप cache होने के बजाय।
- किसी ऐसी property के लिए
varउपयोग करना जिसे class के बाहर से कभी बदलना नहीं चाहिए, private setter या backing field वालेvalके बजाय।
val/varसे declared एक property को पीछे से अपने आप एक generated getter (औरvarके लिए setter) मिल जाता है।- Custom getters और setters property declaration के ठीक बाद
get() = ...औरset(value) { ... }से लिखे जा सकते हैं। - एक custom getter हर बार property पढ़े जाने पर अपने expression को फिर से evaluate करता है; यह cache नहीं होता।
varproperty परprivate setpublic reads की अनुमति देता है लेकिन writes को class के अंदर तक सीमित करता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: