← Back to Swift Course | Chapter 8: Structs & Classes | Lesson 2 of 9

Classes के Basics

एक class struct के cousin जैसी है -- custom चीज़ों के लिए भी एक blueprint -- लेकिन इसकी copy रखने वाला हर कोई असल में बिल्कुल वही object share करता है।
Syntax
swift
class ClassName {
    var property: Type

    init(property: Type) {
        self.property = property
    }
}

let instance = ClassName(property: value)

Defining and Using a Class

एक class struct की तरह properties और methods group करता है, लेकिन इसकी stored properties setup करने के लिए एक explicit initializer चाहता है।

उदाहरण: Defining and Using a Class

markup
// Define a class `Car`
class Car {
    var brand: String
    var speed: Int
    init(brand: String, speed: Int) {
        self.brand = brand
        self.speed = speed
    }
}
// Declare a constant `myCar`, set to `Car(brand: "Toyota", speed: 0)`
let myCar = Car(brand: "Toyota", speed: 0)
// Print "\(myCar.brand) at \(myCar.speed) mph" to the console
print("\(myCar.brand) at \(myCar.speed) mph")

Classes Are Reference Types

एक class instance को किसी दूसरे variable को assign करना इसे copy नहीं करता; दोनों variables बिल्कुल उसी object को refer करते हैं, इसलिए एक के ज़रिए changes दूसरे के ज़रिए भी visible हैं।

उदाहरण: Classes Are Reference Types

markup
// Define a class `Car`
class Car {
    var brand: String
    var speed: Int
    init(brand: String, speed: Int) {
        self.brand = brand
        self.speed = speed
    }
}
// Declare a constant `carA`, set to `Car(brand: "Honda", speed: 0)`
let carA = Car(brand: "Honda", speed: 0)
// Declare a constant `carB`, set to `carA`
let carB = carA
carB.speed = 60
// Print "A speed: \(carA.speed), B speed: \(carB.speed)" to the console
print("A speed: \(carA.speed), B speed: \(carB.speed)")
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. यह भूल जाना कि classes को structs की तरह एक automatic memberwise initializer नहीं मिलता -- आपको अपना खुद का init लिखना चाहिए।
  2. यह मान लेना कि एक class instance को एक नए variable को assign करना इसे copy करता है; classes reference types हैं, इसलिए दोनों variables उसी object को point करते हैं।
  3. == (value equality, custom implementation चाहिए) को === (identity: क्या ये memory में वही object हैं) के साथ confuse करना।
चैप्टर सारांश
  • एक class बहुत कुछ एक struct की तरह declare होता है लेकिन एक reference type है।
  • Classes को अपना खुद का initializer define करना चाहिए; कोई automatic memberwise init नहीं है।
  • एक class instance को assign करना variables के बीच वही underlying object share करता है।
  • === जांचता है कि क्या दो references बिल्कुल उसी instance को point करते हैं।
🔒

Chapter Quiz — Complete all 9 topics to unlock

0/9 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.