You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classPerson{varname="Jone Doe"init(){print("\(name) is alive!")}func printGreeting(){print("Hello, I'm \(name)")}deinit{print("\(name) is no more!")}}
for _ in 1...3{letperson=Person()
person.printGreeting()}
Mutability
classSinger1{varname="Taylor Swift"
// let name = "Taylor Swift"
}lettaylor1=Singer1()
taylor1.name ="Ed Sheeran"print(taylor1.name)
Classes summary
Classes and structs are similar, in that they can both let you create your own types with properties and methods.
One class can inherit from another, and it gains all the properties and methods of the parent class. It’s common to talk about class hierarchies – one class based on another, which itself is based on another.
You can mark a class with the final keyword, which stops other classes from inheriting from it.
Method overriding lets a child class replace a method in its parent class with a new implementation.
When two variables point at the same class instance, they both point at the same piece of memory – changing one changes the other.
Classes can have a deinitializer, which is code that gets run when an instance of the class is destroyed.
Classes don’t enforce constants as strongly as structs – if a property is declared as a variable, it can be changed regardless of how the class instance was created.