← Back to Kotlin Course | Chapter 6: Classes & Objects | Lesson 3 of 7

Properties क्या हैं

Properties वे labeled bits of information हैं जो एक class अपने साथ रखता है, और Kotlin चुपचाप आपके लिए getter और setter बना देता है।
Syntax
kotlin
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

markup
// 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}")
}

Custom Getters

कोई property एक custom get() define कर सकती है जो उस value को सीधे store करने के बजाय दूसरी properties से तुरंत compute करता है।

उदाहरण: Custom Getters

markup
// 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}")
}

Custom Setters

एक var property एक custom set(value) define कर सकती है जो हर बार एक नई value assign होने पर validation या transformation logic चलाता है।

Note: किसी custom setter के अंदर, field property की backing storage को refer करता है।

उदाहरण: Custom Setters

markup
// 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}")
}

Restricting Setter Visibility

किसी var property के बाद private set जोड़ना दूसरे code को इसे स्वतंत्र रूप से पढ़ने देता है जबकि सिर्फ class खुद इसकी value बदल सकती है, जो read-only state को सुरक्षित रूप से उजागर करने का एक आम तरीका है।

उदाहरण: Restricting Setter Visibility

markup
// 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}")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Java की आदत से manual getX()/setX() methods लिखना, जबकि Kotlin properties पहले से उन्हें अपने आप generate कर देती हैं।
  2. यह भूल जाना कि एक custom getter हर बार access होने पर अपनी value फिर से compute करता है, अपने आप cache होने के बजाय।
  3. किसी ऐसी 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 नहीं होता।
  • var property पर private set public reads की अनुमति देता है लेकिन writes को class के अंदर तक सीमित करता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.